Android開發之路——單選框,複選框,彈出框等控制項操作

來源:互聯網
上載者:User

由於這幾個控制項都是比較常用的控制項,所以在進行操作的時候會比較常用,所以這個部分算是Android軟體開發的重要部分,內容比較簡單。分類型進行介紹

1.單選框操作:單選框在Android裡面隨處可見,它是由兩部分組成的,一部分是RadioGroup,一部分是RadioButton。一個RadioGroup裡面是有多個RadioButton。每個RadioButton就是一個單選項,而控制的時候是控制RadioGroup。下面是Xml和代碼的實現部分

xml:

  1. <RadioGroup  
  2.         Android:id = "@+id/genderGroup"  
  3.         Android:layout_width = "wrap_content"  
  4.         Android:layout_height = "wrap_content"  
  5.         Android:orientation = "horizontal"  
  6.         >  
  7.           
  8.         <RadioButton  
  9.             Android:id = "@+id/femaleButton"  
  10.             Android:layout_width = "wrap_content"  
  11.             Android:layout_height = "wrap_content"  
  12.             Android:text = "@string/female"/>  
  13.   
  14.         <RadioButton  
  15.             Android:id = "@+id/maleButton"  
  16.             Android:layout_width = "wrap_content"  
  17.             Android:layout_height = "wrap_content"  
  18.             Android:text = "@string/male"/>  
  19.     </RadioGroup>  

上面定義了一個簡單的RadioGroup和RadioButton的顯示。

下面是綁定這個控制項的事件的作業碼:

  1. //通過綁定genderGroup的OnCheckedChangeListener的方法來進行事件響應   
  2.        genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {  
  3.           
  4.         @Override  
  5.         public void onCheckedChanged(RadioGroup group, int checkedId) {  
  6.             if(femaleButton.getId() ==checkedId){  
  7.                 System.out.println("female");  
  8.                 Toast.makeText(Activity07.this, "女", Toast.LENGTH_SHORT).show();  
  9.             }else if(maleButton.getId() == checkedId){  
  10.                 System.out.println("male");  
  11.                 Toast.makeText(Activity07.this, "男", Toast.LENGTH_SHORT).show();  
  12.             }  
  13.         }  
  14.     });  

2.彈出框(Toast)彈出框的事件跟Swing的JOptionPane很像,但是它是叫Toast,使用的方法就是在你需要彈出資訊的地方加上:Toast.makeText(這裡是你要彈出的類對象名,這個是你要彈出的字串 , 這個是你要彈出的長度大小)。具體例子看上面一段Java代碼的最後一行。彈出框不需要在xml裡面進行配置。

3.複選框(checkBox):複選框就沒有單選框那樣有組的概念了,所以複選框的操作和單選框比起來就會比較複雜一點點,因為你要對每個複選框都進行一個事件響應。下面是一個複選框的例子。

  1. <CheckBox  
  2.         Android:id = "@+id/swim"  
  3.         Android:layout_width = "wrap_content"  
  4.         Android:layout_height = "wrap_content"  
  5.         Android:text = "@string/swim"/>  
  6.       
  7.     <CheckBox  
  8.         Android:id = "@+id/run"  
  9.         Android:layout_width = "wrap_content"  
  10.         Android:layout_height = "wrap_content"  
  11.         Android:text = "@string/run"/>  
  12.       
  13.     <CheckBox  
  14.         Android:id = "@+id/read"  
  15.         Android:layout_width = "wrap_content"  
  16.         Android:layout_height = "wrap_content"  
  17.         Android:text = "@string/read"/>  

下面是時間響應的代碼:

  1. //綁定checkBox的監聽器和radioGroup的方法是不一樣的,要注意區別   
  2.         swimCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {  
  3.               
  4.             @Override  
  5.             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
  6.                 if (isChecked) {  
  7.                     System.out.println("swim is checked");  
  8.                 }else {  
  9.                     System.out.println("swim is unChecked");  
  10.                 }  
  11.             }  
  12.         });  
  13.         runCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {  
  14.               
  15.             @Override  
  16.             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
  17.                 if (isChecked) {  
  18.                     System.out.println("run is checked");  
  19.                 }else {  
  20.                     System.out.println("run is unChecked");  
  21.                 }  
  22.             }  
  23.         });  
  24.         readCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {  
  25.               
  26.             @Override  
  27.             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
  28.                 if (isChecked) {  
  29.                     System.out.println("read is checked");  
  30.                 }else {  
  31.                     System.out.println("read is unChecked");  
  32.                 }  
  33.             }  
  34.         });  
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.