checkbox就是选择框。

效果:

 

layout中的xml文件:

 


  
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7. <TextView    
  8.     android:id="@+id/textview1" 
  9.     android:layout_width="fill_parent"   
  10.     android:layout_height="wrap_content"   
  11.     android:text="选择性别:" 
  12.     /> 
  13. <CheckBox 
  14.     android:id="@+id/Checkbox1" 
  15.     android:layout_width="wrap_content" 
  16.     android:layout_height="wrap_content" 
  17.     android:text="男" 
  18. /> 
  19. <CheckBox 
  20.     android:id="@+id/Checkbox2" 
  21.     android:layout_width="wrap_content" 
  22.     android:layout_height="wrap_content" 
  23.     android:text="女" 
  24. />   
  25. //
  26. <RadioGroup   
  27.     android:id="@+id/myRadioG" 
  28.     android:layout_width="wrap_content" 
  29.     android:layout_height="wrap_content"> 
  30.     <RadioButton   
  31.         android:id="@+id/myRadio1" 
  32.         android:layout_width="wrap_content" 
  33.         android:layout_height="wrap_content" 
  34.         android:text="篮球"/> 
  35.     <RadioButton   
  36.         android:id="@+id/myRadio2" 
  37.         android:layout_width="wrap_content" 
  38.         android:layout_height="wrap_content" 
  39.         android:text="足球"/> 
  40.     <RadioButton 
  41.         android:id="@+id/myRadio3" 
  42.         android:layout_width="wrap_content" 
  43.         android:layout_height="wrap_content" 
  44.         android:text="羽毛球"/> 
  45. </RadioGroup> 
  46. </LinearLayout> 

java文件:

 


  
  1. package com.cheng.checkbox;  
  2.  
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.CheckBox;  
  9. import android.widget.CompoundButton;  
  10. import android.widget.RadioButton;  
  11. import android.widget.RadioGroup;  
  12. import android.widget.Toast;  
  13. import android.widget.CompoundButton.OnCheckedChangeListener;  
  14.  
  15. public class CheckBoxActivity extends Activity {  
  16.     private CheckBox mCheckBox1;  
  17.     private  CheckBox mCheckBox2;  
  18.     private RadioGroup mRadioGroup;  
  19.     private RadioButton mRadioButton1;  
  20.     private RadioButton mRadioButton2;  
  21.     private RadioButton mRadioButton3;  
  22.       
  23.     @Override 
  24.     protected void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         super.setContentView(R.layout.main);  
  27.           
  28.         mCheckBox1 = (CheckBox)findViewById(R.id.Checkbox1);  
  29.         mCheckBox2 = (CheckBox)findViewById(R.id.Checkbox2);  
  30.           
  31.         mRadioGroup = (RadioGroup)findViewById(R.id.myRadioG);  
  32.           
  33.         mRadioButton1 = (RadioButton)findViewById(R.id.myRadio1);  
  34.         mRadioButton2 = (RadioButton)findViewById(R.id.myRadio2);  
  35.         mRadioButton3 = (RadioButton)findViewById(R.id.myRadio3);  
  36.           
  37.         OnClickListener ol = new OnClickListener() {  
  38.              //设置监听 
  39.             @Override 
  40.             public void onClick(View v) {  
  41.                 // TODO Auto-generated method stub  
  42.                 switch (v.getId()) {  
  43.                 case R.id.Checkbox1:  
  44. //将选中的Radio和Checkbox的内容用Toast显示出来,第一个参数是用来显示的组件,第二个参数是要显示的内容,第三个参数是显示的时间
  45.                     Toast.makeText(CheckBoxActivity.this"choose "+(((CheckBox)v).getId()), Toast.LENGTH_LONG).show();  
  46.                     break;  
  47.                 case R.id.Checkbox2:  
  48.                     Toast.makeText(CheckBoxActivity.this"choose"+(((CheckBox)v).getId()), Toast.LENGTH_LONG).show();  
  49.                     break;  
  50.                 case R.id.myRadio1:  
  51.                     Toast.makeText(CheckBoxActivity.this"choose"+(((CheckBox)v).getId()), Toast.LENGTH_LONG).show();  
  52.                     break;  
  53.                 case R.id.myRadio2:  
  54.                     Toast.makeText(CheckBoxActivity.this"choose"+(((CheckBox)v).getId()), Toast.LENGTH_LONG).show();  
  55.                     break;  
  56.                 case R.id.myRadio3:  
  57.                     Toast.makeText(CheckBoxActivity.this"choose"+(((CheckBox)v).getId()), Toast.LENGTH_LONG).show();  
  58.                     break;  
  59.                   
  60.  
  61.                 default:  
  62.                     break;  
  63.                 }  
  64.             }  
  65.         };  
  66.          //设置监听,当checkbox和RadioButton的选择发生改变的时候发生
  67.         OnCheckedChangeListener occl = new OnCheckedChangeListener() {  
  68.               
  69.             @Override 
  70.             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
  71.                 // TODO Auto-generated method stub  
  72.                 if(isChecked)  
  73.                 {  
  74.                     Toast.makeText(getApplicationContext(), buttonView.getText(), Toast.LENGTH_LONG).show();  
  75.                 }  
  76.             }  
  77.         };  
  78.           
  79.         //将监听事件加到RadioButton和CheckBox组件上
  80.         mCheckBox1.setOnClickListener(ol);  
  81.         mCheckBox2.setOnClickListener(ol);  
  82.         mCheckBox1.setOnCheckedChangeListener(occl);  
  83.         mCheckBox2.setOnCheckedChangeListener(occl);  
  84.           
  85.         mRadioButton1.setOnCheckedChangeListener(occl);  
  86.         mRadioButton2.setOnCheckedChangeListener(occl);  
  87.         mRadioButton3.setOnCheckedChangeListener(occl);  
  88.           
  89.           
  90.     }  
  91.  
  92.     @Override 
  93.     public boolean onCreateOptionsMenu(Menu menu) {  
  94.         // Inflate the menu; this adds items to the action bar if it is present.  
  95.         getMenuInflater().inflate(R.menu.main, menu);  
  96.         return true;  
  97.     }  
  98.  
  99. }