星空网 > 软件开发 > 操作系统

RadioButton与CheckBox

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

RadioButton(单选)和CheckBox(多选)
  1. 先来看看继承关系吧
    1. RadioButton与CheckBoximages/loading.gif' data-original="http://images2015.cnblogs.com/blog/752718/201611/752718-20161113182144952-1781026133.png" width="322" height="169" border="0"> 
    2. 两个还是亲兄弟,是View的第四代传人,是View的玄孙,好小呀!
  2. RadioButton必须按组来分,而CheckBox不用,可以自由的玩耍;
    1. 这里就需要引入了圈养人RadioGroup
    2. RadioButton与CheckBox
    3. 这里不难看出,圈养人是LinearLayout的儿子,那么就可以制定方向了
    4. 上边写RadioButton必须按组来分,这里语义上有点歧义吧,其实他可以不需要圈养人,也可以单独存在,但是单独存在就是去了意义,因为如果没有RadioGroup,每个RadioButton都可以点中,互相之间没有依赖了;
  3. 上代码,熟悉下
    1. RadioButton与CheckBox
    2. 布局
      1. <?="1.0" encoding="utf-8"?>
      2. <LinearLayout :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当初设计的还是怕把这两个方法写在一个接口中,担心回调的时候出现混淆吧;



来自为知笔记(Wiz)






原标题:RadioButton与CheckBox

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

最新!热门动漫大IP火影忍者 NARUTO首发跨境侵权和解案!:https://www.ikjzd.com/articles/1658054310401175554
干货 | 亚马逊品牌备案注册步骤:https://www.ikjzd.com/articles/16582
独立站引流方式有哪些?跨境电商独立网站流量不足的解决措施与方法探究:https://www.ikjzd.com/articles/1658267575370063873
注册德国公司有什么好处和优势_德国公司注册的条件_德国公司注册需提交相关资料:https://www.ikjzd.com/articles/1658287445793599489
做外贸如何应对客户询价后的反应:https://www.ikjzd.com/articles/1658288675760054273
中国法人如何进行德国公司注册?:https://www.ikjzd.com/articles/1658295811898548225
TikTok 将推出先买后付服务 :https://www.goluckyvip.com/news/188219.html
深圳有没有比较好玩的景点 深圳有没有比较好玩的景点推荐一下:https://www.vstour.cn/a/366175.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流