笔者长期从事于数据库的开发,算了,不提当年了,因为一直用的是小语种(PowerBuilder),还是来说说这两个最常见的控件吧!

RadioButton(单选)和CheckBox(多选)

  1. 先来看看继承关系吧
  2.  
  3. 两个还是亲兄弟,是View的第四代传人,是View的玄孙,好小呀!
RadioButton必须按组来分,而CheckBox不用,可以自由的玩耍;
  1. 这里就需要引入了圈养人RadioGroup
  2. 这里不难看出,圈养人是LinearLayout的儿子,那么就可以制定方向了
  3. 上边写RadioButton必须按组来分,这里语义上有点歧义吧,其实他可以不需要圈养人,也可以单独存在,但是单独存在就是去了意义,因为如果没有RadioGroup,每个RadioButton都可以点中,互相之间没有依赖了;
上代码,熟悉下
  1. 布局 <?xml version="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. android:padding="10dp">
  7. <TextView
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:text="请选择你的性别"/>
  11. <RadioGroup
  12. android:id="@+id/rg_sex"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:orientation="horizontal">
  16. <RadioButton
  17. android:id="@+id/rb_man"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:text="男"/>
  21. <RadioButton
  22. android:id="@+id/rb_woman"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:text="女"/>
  26. </RadioGroup>
  27. <TextView
  28. android:layout_width="match_parent"
  29. android:layout_height="wrap_content"
  30. android:text="请选择你的爱好"/>
  31. <CheckBox
  32. android:id="@+id/cb_swimming"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:text="游泳"/>
  36. <CheckBox
  37. android:id="@+id/cb_running"
  38. android:layout_width="wrap_content"
  39. android:layout_height="wrap_content"
  40. android:text="跑步"/>
  41. <CheckBox
  42. android:id="@+id/cb_study"
  43. android:layout_width="wrap_content"
  44. android:layout_height="wrap_content"
  45. android:text="学习"/>
  46. </LinearLayout>

代码

  1. publicclassCheckboxAndRadioBoxActivityextendsAppCompatActivityimplementsCompoundButton.OnCheckedChangeListener,
  2. RadioGroup.OnCheckedChangeListener{
  3. privateRadioGrouprg_sex;
  4. privateCheckBoxcb_swimming;
  5. privateCheckBoxcb_running;
  6. privateCheckBoxcb_study;
  7. privateList<String>hobby=newArrayList<>();
  8. @Override
  9. protectedvoidonCreate(@NullableBundlesavedInstanceState){
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_checkbox_and_radiobox);
  12. rg_sex=(RadioGroup)findViewById(R.id.rg_sex);
  13. cb_swimming=(CheckBox)findViewById(R.id.cb_swimming);
  14. cb_running=(CheckBox)findViewById(R.id.cb_running);
  15. cb_study=(CheckBox)findViewById(R.id.cb_study);
  16. rg_sex.setOnCheckedChangeListener(this);
  17. cb_swimming.setOnCheckedChangeListener(this);
  18. cb_running.setOnCheckedChangeListener(this);
  19. cb_study.setOnCheckedChangeListener(this);
  20. }
  21. @Override
  22. publicvoidonCheckedChanged(CompoundButtonbuttonView,booleanisChecked){
  23. switch(buttonView.getId()){
  24. caseR.id.cb_swimming:
  25. if(isChecked){
  26. hobby.add("游泳");
  27. }else{
  28. hobby.remove("游泳");
  29. }
  30. break;
  31. caseR.id.cb_running:
  32. if(isChecked){
  33. hobby.add("跑步");
  34. }else{
  35. hobby.remove("跑步");
  36. }
  37. break;
  38. caseR.id.cb_study:
  39. if(isChecked){
  40. hobby.add("学习");
  41. }else{
  42. hobby.remove("学习");
  43. }
  44. break;
  45. }
  46. Stringstr="";
  47. for(inti=0;i<hobby.size();i++){
  48. if(i==0){
  49. str=hobby.get(0);
  50. }else{
  51. str+=","+hobby.get(i);
  52. }
  53. }
  54. Toast.makeText(getApplicationContext(),"爱好:"+str,Toast.LENGTH_SHORT).show();
  55. }
  56. @Override
  57. publicvoidonCheckedChanged(RadioGroupgroup,intcheckedId){
  58. switch(checkedId){
  59. caseR.id.rb_man:
  60. Toast.makeText(getApplicationContext(),"性别:男",Toast.LENGTH_SHORT).show();
  61. break;
  62. caseR.id.rb_woman:
  63. Toast.makeText(getApplicationContext(),"性别:女",Toast.LENGTH_SHORT).show();
  64. break;
  65. }
  66. }
  67. }

这里说说当RadioGroup与CompoundButton的OnCheckedChangeListener出现冲突怎么办?他们的方法都是onCheckedChanged,我这里用Activity同时实现以上两个接口,根据参数的不同,RadioGroup与CompoundButton他们知道自己去调用自己的接口方法,虽然名称一样,但是参数不同呀,这里就要理解两个概念了

  1. 方法重载:方法名称相同,与返回值无关,参数个数或者类型至少有一样不同
  2. 方法覆盖: 子类重写了父类的方法,函数名称,参数,返回值必须和父类一模一样,这里可以加注解来判断@Override,系统可以帮你检测方法 的正确性;
  3. 当初老师的说法是既然这两个接口一样,为什么不写成一个接口,这岂不是违背了面向对象的原理?我认为主要从两方面来考虑:
  4. RadioGroup.OnCheckedChangeListener是对组的一个点击改变的监听,CompoundButton.OnCheckedChangeListener是对view组件的点击事件改变的监听,group可以有孩子,而后者不可以有孩子,我想这应该是本质的区别吧;
  5. 我想Google当初设计的还是怕把这两个方法写在一个接口中,担心回调的时候出现混淆吧;

来自为知笔记(Wiz)