zoukankan      html  css  js  c++  java
  • RadioButton与CheckBox

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

    RadioButton(单选)和CheckBox(多选)
    1. 先来看看继承关系吧
      1.  
      2. 两个还是亲兄弟,是View的第四代传人,是View的玄孙,好小呀!
    2. RadioButton必须按组来分,而CheckBox不用,可以自由的玩耍;
      1. 这里就需要引入了圈养人RadioGroup
      2. 这里不难看出,圈养人是LinearLayout的儿子,那么就可以制定方向了
      3. 上边写RadioButton必须按组来分,这里语义上有点歧义吧,其实他可以不需要圈养人,也可以单独存在,但是单独存在就是去了意义,因为如果没有RadioGroup,每个RadioButton都可以点中,互相之间没有依赖了;
      4. 默认的RadioButton的圆形选择框在前面,如果想放到后面,可以在RadioButton的xml中加上
        1. android:button="@null"
        2. android:drawableRight="@android:drawable/btn_radio"
         
    3. 上代码,熟悉下
      1. 布局
        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>
         
      2. 代码
        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当初设计的还是怕把这两个方法写在一个接口中,担心回调的时候出现混淆吧;





    世界上没有什么事情是跑步解决不了的,如果有,那就再跑一会!
  • 相关阅读:
    python中的scapy模块
    延时注入跨库注入
    Linux 技巧:让进程在后台可靠运行的几种方法
    爆破phpmyadmin小脚本
    ACID原则
    MYSQL批量插入数据库实现语句性能分析
    MYSQL批量插入数据库实现语句性能分析
    JS数组去重算法
    JS数组去重算法
    JS中substr和substring的用法和区别
  • 原文地址:https://www.cnblogs.com/his365/p/6073323.html
Copyright © 2011-2022 走看看