博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
RadioButton与CheckBox_优就业
阅读量:4290 次
发布时间:2019-05-27

本文共 4138 字,大约阅读时间需要 13 分钟。

长期从事于Android(http://www.ujiuye.com/)的开发,算了,不提当年了,因为一直用的是小语种(PowerBuilder),还是来说说这两个最常见的控件吧!

RadioButton(单选)和CheckBox(多选)

  1. 先来看看继承关系吧

    1. RadioButton与CheckBox_优就业

    2. 两个还是亲兄弟,是View的第四代传人,是View的玄孙,好小呀!

  2. RadioButton必须按组来分,而CheckBox不用,可以自由的玩耍;

    1. 这里就需要引入了圈养人RadioGroup

    2. RadioButton与CheckBox_优就业

    3. 这里不难看出,圈养人是LinearLayout的儿子,那么就可以制定方向了

    4. 上边写RadioButton必须按组来分,这里语义上有点歧义吧,其实他可以不需要圈养人,也可以单独存在,但是单独存在就是去了意义,因为如果没有RadioGroup,每个RadioButton都可以点中,互相之间没有依赖了;

    5. 默认的RadioButton的圆形选择框在前面,如果想放到后面,可以在RadioButton的xml中加上

      1. android:button="@null"

      2. android:drawableRight="@android:drawable/btn_radio"

  3. 上代码,熟悉下

    1. RadioButton与CheckBox_优就业

    2. 布局

      1. <?xml version="1.0" encoding="utf-8"?>

      2. <LinearLayout xmlns: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>

    3. 代码

      1. publicclassCheckboxAndRadioBoxActivityextendsAppCompatActivityimplementsCompoundButton.OnCheckedChangeListener,

      2. RadioGroup.OnCheckedChangeListener{

      3. privateRadioGroup rg_sex;

      4. privateCheckBox cb_swimming;

      5. privateCheckBox cb_running;

      6. privateCheckBox cb_study;

      7. privateList<String> hobby =newArrayList<>();

      8. @Override

      9. protectedvoid onCreate(@NullableBundle savedInstanceState){

      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. publicvoid onCheckedChanged(CompoundButton buttonView,boolean isChecked){

      23. switch(buttonView.getId()){

      24. case R.id.cb_swimming:

      25. if(isChecked){

      26. hobby.add("游泳");

      27. }else{

      28. hobby.remove("游泳");

      29. }

      30. break;

      31. case R.id.cb_running:

      32. if(isChecked){

      33. hobby.add("跑步");

      34. }else{

      35. hobby.remove("跑步");

      36. }

      37. break;

      38. case R.id.cb_study:

      39. if(isChecked){

      40. hobby.add("学习");

      41. }else{

      42. hobby.remove("学习");

      43. }

      44. break;

      45. }

      46. String str="";

      47. for(int i =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. publicvoid onCheckedChanged(RadioGroup group,int checkedId){

      58. switch(checkedId){

      59. case R.id.rb_man:

      60. Toast.makeText(getApplicationContext(),"性别:男",Toast.LENGTH_SHORT).show();

      61. break;

      62. case R.id.rb_woman:

      63. Toast.makeText(getApplicationContext(),"性别:女",Toast.LENGTH_SHORT).show();

      64. break;

      65. }

      66. }

      67. }

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

    1. 方法重载:方法名称相同,与返回值无关,参数个数或者类型至少有一样不同

    2. 方法覆盖: 子类重写了父类的方法,函数名称,参数,返回值必须和父类一模一样,这里可以加注解来判断@Override,系统可以帮你检测方法 的正确性;

    3. 当初老师的说法是既然这两个接口一样,为什么不写成一个接口,这岂不是违背了面向对象的原理?我认为主要从两方面来考虑:

      1. RadioGroup.OnCheckedChangeListener是对组的一个点击改变的监听,CompoundButton.OnCheckedChangeListener是对view组件的点击事件改变的监听,group可以有孩子,而后者不可以有孩子,我想这应该是本质的区别吧;

      2. 我想Google当初设计的还是怕把这两个方法写在一个接口中,担心回调的时候出现混淆吧;

        更多Android开发知识尽在优就业IT培训:http://www.ujiuye.com/

转载地址:http://stggi.baihongyu.com/

你可能感兴趣的文章
全面理解Java内存模型
查看>>
Java中Synchronized的用法
查看>>
阻塞队列
查看>>
linux的基础知识
查看>>
接口技术原理
查看>>
五大串口的基本原理
查看>>
PCB设计技巧与注意事项
查看>>
linux进程之间通讯常用信号
查看>>
main函数带参数
查看>>
PCB布线技巧
查看>>
关于PCB设计中过孔能否打在焊盘上的两种观点
查看>>
PCB反推理念
查看>>
男人身上必须有的15种修养,让你乘风破浪!
查看>>
stm32关于BOOT0和BOOT1设置
查看>>
向犹太人学习:销售营销高招!
查看>>
fatal error C1010: 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include "stdafx.h
查看>>
电脑死机的情况与避免策略
查看>>
ARM11处理器中的流水线级数增加研究
查看>>
51单片机多任务机制的实现策略
查看>>
编译安装gdb+insight和gdbserver远程调试
查看>>