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

本文详细介绍了Android开发中使用的RadioButton和CheckBox的基本概念、实现方式及具体实例,包括如何通过RadioGroup和RadioGroup配合使用实现单选功能,以及如何通过事件监听器对单选按钮进行操作。此外,文章还展示了如何改变RadioButton的样式,以及CheckBox的复选功能与事件监听机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.RadioButton单选按钮

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

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

下面的具体的例子:

MainActivity.java

 
  1. packagecom.android.radiobutton;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.widget.RadioGroup;
  5. importandroid.widget.Toast;
  6. publicclassMainActivityextendsActivity{
  7. //声明RadioGroup
  8. RadioGroupraGroup1,raGroup2;
  9. @Override
  10. publicvoidonCreate(BundlesavedInstanceState){
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.main);
  13. //通过findViewById获得RadioGroup对象
  14. raGroup1=(RadioGroup)findViewById(R.id.radioGroup1);
  15. //添加事件监听器
  16. raGroup1.setOnCheckedChangeListener(newRadioGroup.OnCheckedChangeListener(){
  17. @Override
  18. publicvoidonCheckedChanged(RadioGroupgroup,intcheckedId){
  19. //TODOAuto-generatedmethodstub
  20. if(checkedId==R.id.radioBtn1){
  21. Toast.makeText(MainActivity.this,"你来自广东省",Toast.LENGTH_LONG).show();
  22. }
  23. elseif(checkedId==R.id.radioBtn2){
  24. Toast.makeText(MainActivity.this,"你来自广西省",Toast.LENGTH_LONG).show();
  25. }
  26. else{
  27. Toast.makeText(MainActivity.this,"你来自湖南省",Toast.LENGTH_LONG).show();
  28. }
  29. }
  30. });
  31. raGroup2=(RadioGroup)findViewById(R.id.radioGroup2);
  32. raGroup2.setOnCheckedChangeListener(newRadioGroup.OnCheckedChangeListener(){
  33. @Override
  34. publicvoidonCheckedChanged(RadioGroupgroup,intcheckedId){
  35. //TODOAuto-generatedmethodstub
  36. if(checkedId==R.id.radioBtn4){
  37. Toast.makeText(MainActivity.this,"你的性别是男",Toast.LENGTH_LONG).show();
  38. }
  39. else{
  40. Toast.makeText(MainActivity.this,"你的性别是女",Toast.LENGTH_LONG).show();
  41. }
  42. }
  43. });
  44. }
  45. }

main.xml

 
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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. packagecom.android.checkbox;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.widget.CheckBox;
  5. importandroid.widget.CompoundButton;
  6. importandroid.widget.Toast;
  7. importandroid.widget.CompoundButton.OnCheckedChangeListener;
  8. publicclassMainActivityextendsActivity{
  9. //声明复选按钮
  10. privateCheckBoxcBox1;
  11. privateCheckBoxcBox2;
  12. privateCheckBoxcBox3;
  13. @Override
  14. publicvoidonCreate(BundlesavedInstanceState){
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.main);
  17. //通过findViewById获得CheckBox对象
  18. cBox1=(CheckBox)findViewById(R.id.checkbox1);
  19. cBox2=(CheckBox)findViewById(R.id.checkbox2);
  20. cBox3=(CheckBox)findViewById(R.id.checkbox3);
  21. //注册事件监听器
  22. cBox1.setOnCheckedChangeListener(listener);
  23. cBox2.setOnCheckedChangeListener(listener);
  24. cBox3.setOnCheckedChangeListener(listener);
  25. }
  26. //响应事件
  27. privateOnCheckedChangeListenerlistener=newOnCheckedChangeListener(){
  28. @Override
  29. publicvoidonCheckedChanged(CompoundButtonbuttonView,booleanisChecked)
  30. {
  31. //cBox1被选中
  32. if(buttonView.getId()==R.id.checkbox1){
  33. if(isChecked){
  34. Toast.makeText(MainActivity.this,"你喜欢足球",Toast.LENGTH_LONG).show();
  35. }
  36. }
  37. //cBox2被选中
  38. elseif(buttonView.getId()==R.id.checkbox2){
  39. if(isChecked){
  40. Toast.makeText(MainActivity.this,"你喜欢篮球",Toast.LENGTH_LONG).show();
  41. }
  42. }
  43. //cBox3被选中
  44. elseif(buttonView.getId()==R.id.checkbox3){
  45. if(isChecked){
  46. Toast.makeText(MainActivity.this,"你喜欢排球",Toast.LENGTH_LONG).show();
  47. }
  48. }
  49. }
  50. };
  51. }

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中的起始位置


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值