Android开发学习笔记:RadioButton和CheckBox浅析

来源:互联网 发布:windows xp摄像头驱动 编辑:程序博客网 时间:2024/04/30 08:42

 一.RadioButton单选按钮  

    RadioButton(单选按钮)在Android开发中应用的非常广泛,比如一些选择项的时候,会用到单选按钮。它是一种单个圆形单选框双状态的按钮,可以选择或不选择。在RadioButton没有被选中时,用户能够按下或点击来选中它。但是,与复选框相反,用户一旦选中就不能够取消选中。

    实现RadioButton由两部分组成,也就是RadioButton和RadioGroup配合使用.RadioGroup是单选组合框,可以容纳多个RadioButton的容器.在没有RadioGroup的情况下,RadioButton可以全部都选中;当多个RadioButton被RadioGroup包含的情况下,RadioButton只可以选择一个。并用setOnCheckedChangeListener来对单选按钮进行监听

下面的具体的例子:

MainActivity.java

  1. package com.android.radiobutton;  
  2.  
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.RadioGroup;  
  6. import android.widget.Toast;  
  7.  
  8. public class MainActivity extends Activity {  
  9.       
  10.     //声明RadioGroup  
  11.     RadioGroup raGroup1,raGroup2;  
  12.     @Override 
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.           
  17.         //通过findViewById获得RadioGroup对象  
  18.         raGroup1=(RadioGroup)findViewById(R.id.radioGroup1);       
  19.         //添加事件监听器  
  20.         raGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {  
  21.               
  22.             @Override 
  23.             public void onCheckedChanged(RadioGroup group, int checkedId) {  
  24.                 // TODO Auto-generated method stub  
  25.                 if(checkedId==R.id.radioBtn1){                    
  26.                     Toast.makeText(MainActivity.this"你来自广东省", Toast.LENGTH_LONG).show();  
  27.                 }  
  28.                 else if(checkedId==R.id.radioBtn2){  
  29.                     Toast.makeText(MainActivity.this"你来自广西省", Toast.LENGTH_LONG).show();  
  30.                 }  
  31.                 else{  
  32.                     Toast.makeText(MainActivity.this"你来自湖南省", Toast.LENGTH_LONG).show();  
  33.                 }  
  34.             }  
  35.         });  
  36.         raGroup2=(RadioGroup)findViewById(R.id.radioGroup2);       
  37.         raGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {  
  38.               
  39.             @Override 
  40.             public void onCheckedChanged(RadioGroup group, int checkedId) {  
  41.                 // TODO Auto-generated method stub  
  42.                 if(checkedId==R.id.radioBtn4){                    
  43.                     Toast.makeText(MainActivity.this"你的性别是男", Toast.LENGTH_LONG).show();  
  44.                 }  
  45.                 else {  
  46.                     Toast.makeText(MainActivity.this"你的性别是女", Toast.LENGTH_LONG).show();  
  47.                 }  
  48.             }  
  49.         });         
  50.     }  

main.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:layout_width="fill_parent"   
  9.         android:layout_height="wrap_content"   
  10.         android:text="@string/hello1" 
  11.         /> 
  12.         <RadioGroup 
  13.             android:id="@+id/radioGroup1" 
  14.             android:layout_width="wrap_content" 
  15.             android:layout_height="wrap_content" 
  16.             android:orientation="vertical" 
  17.             > 
  18.             <RadioButton 
  19.                 android:id="@+id/radioBtn1" 
  20.                 android:layout_width="wrap_content" 
  21.                 android:layout_height="wrap_content" 
  22.                 android:text="@string/radioBtn1" 
  23.                /> 
  24.             <RadioButton 
  25.                 android:id="@+id/radioBtn2" 
  26.                 android:layout_width="wrap_content" 
  27.                 android:layout_height="wrap_content" 
  28.                 android:text="@string/radioBtn2" 
  29.                /> 
  30.             <RadioButton 
  31.                 android:id="@+id/radioBtn3" 
  32.                 android:layout_width="wrap_content" 
  33.                 android:layout_height="wrap_content" 
  34.                 android:text="@string/radioBtn3" 
  35.                /> 
  36.         </RadioGroup> 
  37.     <!-- 在两个RadioGroup之间画条横线 --> 
  38.     <View   
  39.         android:layout_width="match_parent"   
  40.         android:layout_height="1dp" 
  41.         android:background="#ffffff" 
  42.         /> 
  43.     <TextView    
  44.         android:layout_width="fill_parent"   
  45.         android:layout_height="wrap_content"   
  46.         android:text="@string/hello2" 
  47.         /> 
  48.         <RadioGroup 
  49.             android:id="@+id/radioGroup2" 
  50.             android:layout_width="wrap_content" 
  51.             android:layout_height="wrap_content" 
  52.             android:orientation="vertical"    
  53.             > 
  54.             <RadioButton 
  55.                 android:id="@+id/radioBtn4" 
  56.                 android:layout_width="wrap_content" 
  57.                 android:layout_height="wrap_content" 
  58.                 android:text="@string/radioBtn4" 
  59.                 android:textColor="#ffffff" 
  60.                /> 
  61.             <RadioButton 
  62.                 android:id="@+id/radioBtn5" 
  63.                 android:layout_width="wrap_content" 
  64.                 android:layout_height="wrap_content" 
  65.                 android:text="@string/radioBtn5" 
  66.                /> 
  67.         </RadioGroup> 
  68. </LinearLayout> 

strings.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="hello1">你来自哪个省</string>
  4. <string name="hello2">你的性别是</string>
  5. <string name="app_name">单选按钮测试</string>
  6. <string name="radioBtn1">广东</string>
  7. <string name="radioBtn2">广西</string>
  8. <string name="radioBtn3">湖南</string>
  9. <string name="radioBtn4"></string>
  10. <string name="radioBtn5"></string>
  11. </resources>

效果图:

RadioButton的另一种效果:

要实现上面的效果,只要在main.xml布局文件中的<RadioButton/>加入android:button="@null"                    
android:drawableRight="@android:drawable/btn_radio"即可,代码如下所示:

  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:layout_width="fill_parent"   
  9.         android:layout_height="wrap_content"   
  10.         android:text="@string/hello1" 
  11.         /> 
  12.         <RadioGroup 
  13.             android:id="@+id/radioGroup1" 
  14.             android:layout_width="wrap_content" 
  15.             android:layout_height="wrap_content" 
  16.             android:orientation="vertical" 
  17.             > 
  18.             <RadioButton 
  19.                 android:id="@+id/radioBtn1" 
  20.                 android:layout_width="wrap_content" 
  21.                 android:layout_height="wrap_content" 
  22.                 android:text="@string/radioBtn1" 
  23.                 android:button="@null"                      
  24.                 android:drawableRight="@android:drawable/btn_radio" 
  25.                /> 
  26.             <RadioButton 
  27.                 android:id="@+id/radioBtn2" 
  28.                 android:layout_width="wrap_content" 
  29.                 android:layout_height="wrap_content" 
  30.                 android:text="@string/radioBtn2" 
  31.                 android:button="@null"                      
  32.                 android:drawableRight="@android:drawable/btn_radio" 
  33.                /> 
  34.             <RadioButton 
  35.                 android:id="@+id/radioBtn3" 
  36.                 android:layout_width="wrap_content" 
  37.                 android:layout_height="wrap_content" 
  38.                 android:text="@string/radioBtn3" 
  39.                 android:button="@null"                      
  40.                 android:drawableRight="@android:drawable/btn_radio" 
  41.                /> 
  42.         </RadioGroup> 
  43.     <!-- 在两个RadioGroup之间画条横线 --> 
  44.     <View   
  45.         android:layout_width="match_parent"   
  46.         android:layout_height="1dp" 
  47.         android:background="#ffffff" 
  48.         /> 
  49.     <TextView    
  50.         android:layout_width="fill_parent"   
  51.         android:layout_height="wrap_content"   
  52.         android:text="@string/hello2" 
  53.         /> 
  54.         <RadioGroup 
  55.             android:id="@+id/radioGroup2" 
  56.             android:layout_width="wrap_content" 
  57.             android:layout_height="wrap_content" 
  58.             android:orientation="vertical"    
  59.             > 
  60.             <RadioButton 
  61.                 android:id="@+id/radioBtn4" 
  62.                 android:layout_width="wrap_content" 
  63.                 android:layout_height="wrap_content" 
  64.                 android:text="@string/radioBtn4" 
  65.                 android:textColor="#ffffff" 
  66.                 android:button="@null"                      
  67.                 android:drawableRight="@android:drawable/btn_radio" 
  68.                /> 
  69.             <RadioButton 
  70.                 android:id="@+id/radioBtn5" 
  71.                 android:layout_width="wrap_content" 
  72.                 android:layout_height="wrap_content" 
  73.                 android:text="@string/radioBtn5" 
  74.                 android:button="@null"                      
  75.                 android:drawableRight="@android:drawable/btn_radio" 
  76.                /> 
  77.         </RadioGroup> 
  78. </LinearLayout> 

 二.CheckBox复选按钮

  CheckBox复选按钮是一种有双状态按钮的特殊类型,可以选中或者不选中。可以现在布局文件中定义多选按钮,然后对每一个多选按钮进行事件监setOnCheckedChangeListener,通过isChecked来判断选项是否被选中

 下面是具体的例子:

 MainActivity.java

  1. package com.android.checkbox;  
  2.  
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.CheckBox;  
  6. import android.widget.CompoundButton;  
  7. import android.widget.Toast;  
  8. import android.widget.CompoundButton.OnCheckedChangeListener;  
  9.  
  10. public class MainActivity extends Activity{  
  11.     //声明复选按钮  
  12.     private CheckBox cBox1;  
  13.     private CheckBox cBox2;  
  14.     private CheckBox cBox3;  
  15.       
  16.     @Override 
  17.     public void onCreate(Bundle savedInstanceState){  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.         //通过findViewById获得CheckBox对象  
  21.         cBox1=(CheckBox)findViewById(R.id.checkbox1);  
  22.         cBox2=(CheckBox)findViewById(R.id.checkbox2);  
  23.         cBox3=(CheckBox)findViewById(R.id.checkbox3);  
  24.  
  25.         //注册事件监听器  
  26.         cBox1.setOnCheckedChangeListener(listener);  
  27.         cBox2.setOnCheckedChangeListener(listener);  
  28.         cBox3.setOnCheckedChangeListener(listener);  
  29.  
  30.     }  
  31.     //响应事件  
  32.     private OnCheckedChangeListener listener = new OnCheckedChangeListener(){  
  33.         @Override 
  34.         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)  
  35.         {  
  36.             //cBox1被选中  
  37.             if (buttonView.getId()==R.id.checkbox1){  
  38.                 if (isChecked){  
  39.                     Toast.makeText(MainActivity.this"你喜欢足球", Toast.LENGTH_LONG).show();  
  40.                 }  
  41.             }  
  42.             //cBox2被选中  
  43.             else if (buttonView.getId()==R.id.checkbox2){  
  44.                 if (isChecked){  
  45.                     Toast.makeText(MainActivity.this"你喜欢篮球", Toast.LENGTH_LONG).show();  
  46.                 }  
  47.             }  
  48.             //cBox3被选中  
  49.             else if (buttonView.getId()==R.id.checkbox3){  
  50.                 if (isChecked){  
  51.                     Toast.makeText(MainActivity.this"你喜欢排球", Toast.LENGTH_LONG).show();  
  52.                 }  
  53.             }         
  54.         }  
  55.     };  

main.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:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/hello"
  11. android:textSize="20sp"
  12. android:textStyle="bold"
  13. android:textColor="#FFFFFF"
  14. />
  15. <CheckBox
  16. android:id="@+id/checkbox1"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:text="@string/football"
  20. android:textSize="16sp"
  21. />
  22. <CheckBox
  23. android:id="@+id/checkbox2"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:text="@string/basketball"
  27. android:textSize="16sp"
  28. />
  29. <CheckBox
  30. android:id="@+id/checkbox3"
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:text="@string/volleyball"
  34. android:textSize="16sp"
  35. />
  36. </LinearLayout>

strings.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="hello">你喜欢的运动是</string>
  4. <string name="app_name">复选按钮测试</string>
  5. <string name="football">足球</string>
  6. <string name="basketball">篮球</string>
  7. <string name="volleyball">排球</string>
  8. </resources>

效果图:

 

三.总结

RadioButton和CheckBox的区别:

1、单个RadioButton在选中后,通过点击无法变为未选中单个CheckBox在选中后,通过点击可以变为未选中

2、一组RadioButton,只能同时选中一个一组CheckBox,能同时选中多个

3、RadioButton在大部分UI框架中默认都以圆形表示CheckBox在大部分UI框架中默认都以矩形表示

RadioButton和RadioGroup的关系:

1、RadioButton表示单个圆形单选框,而RadioGroup是可以容纳多个RadioButton的容器

2、每个RadioGroup中的RadioButton同时只能有一个被选中

3、不同的RadioGroup中的RadioButton互不相干,即如果组A中有一个选中了,组B中依然可以有一个被选中

4、一般情况下,一个RadioGroup中至少有2个RadioButton

5、一般情况下,一个RadioGroup中的RadioButton默认会有一个被选中,并建议您将它放在RadioGroup中的起始位置


原创粉丝点击