[基础控件]—状态切换控件CompoundButton及其子类CheckBox、RadioButton、ToggleButton、switch事件监听与场景使用

[基础控件]—状态切换控件CompoundButton及其子类CheckBox、RadioButton、ToggleButton、switch事件监听与场景使用一、事件监听对于普通的Button,对其进行事件监听Google官方给出了常见的三种监听方式:1、对每一个button设置事件监听器button.setOnClickListener(View.Onc

大家好,又见面了,我是你们的朋友全栈君。

一、事件监听

对于普通的Button,对其进行事件监听Google官方给出了常见的三种监听方式:1、对每一个button设置事件监听器button.setOnClickListener(View.OnclickListener  listener);此种方法当button按钮较多时代码显得多、乱、不够简洁明了。

2、在Activity中实现接口View.OnclickListener,然后重写void onClick(View v)方法,在方法中通过switch(v.getId())予以区分不同Button。此种方法较为简洁,但是需要实现View.OnclickListener接口。3、在xml布局中在想要被监听的

button上添加属性:android:onClick=”doClick”属性。在Activity 中添加监听方法public void doClick(View view){},此种方法书写简单、明了、不需要实现额外的接口。推荐使用此种方法。也是Google官方文档中常见用法。

对于状态切换控件CompoundButton,不仅要对事件触发的监听,还有对状态切换的监听。所以在CompoundButton中需要对其进行两个监听:事件触发、状态切换。监听的方式与普通Button三种监听方式相似。只不过是多了一个监听状态的一项

而已。说多了都是废话,还是直接上码。

场景一:对UI界面上多个CompoundButton的事件监听做统一处理。

<ToggleButton
        android:id="@+id/togglebutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="doClick"
        android:textOff="关"
        android:textOn="开" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <CheckBox
            android:id="@+id/checkbox_meat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="doClick"
            android:text="肉" />

        <CheckBox
            android:id="@+id/checkbox_cheese"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="doClick"
            android:text="奶" />
    </LinearLayout>

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/radiobutton_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="doClick"
            android:text="增" />

        <RadioButton
            android:id="@+id/radiobutton_delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="doClick"
            android:text="刪" />

        <RadioButton
            android:id="@+id/radiobutton_update"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="doClick"
            android:text="改" />

        <RadioButton
            android:id="@+id/radiobutton_seach"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="doClick"
            android:text="查" />
    </RadioGroup>

有了布局,下面上Java代码对其所有的CompoundButton控件进行统一监听

/**
     * 向上转型的目的是为了获取子控件当前状态。
     * @param view
     */
    public void doClick(View view) {
        //1、被选中:toogle中isChecked==on
        boolean isChecked=((CompoundButton)view).isChecked();//向上转型:获取当前状态
        //2、被点击
        switch (view.getId()) {
        case R.id.togglebutton:
            if(isChecked){
                Log.i("MyInfo", "开");
            }else{
                Log.i("MyInfo", "关");
            }
            break;
        case R.id.checkbox_meat:
            if(isChecked){
                Log.i("MyInfo", "肉被选中");
            }else{
                Log.i("MyInfo", "肉被取消");
            }
            break;
        case R.id.checkbox_cheese:
            
            break;
        case R.id.radiobutton_add://切记:RadioButton无状态的切换,仅有按钮的切换。所以仅需判断选中状态 if(isChecked)
            if(isChecked)
            break;
        case R.id.radiobutton_delete:
            if(isChecked)
            
            break;
        case R.id.radiobutton_update:
            if(isChecked)
            break;
        case R.id.radiobutton_seach:
            if(isChecked)
            break;
        default:
            break;
        }
    }

在doClick()方法中总体上执行了两个步骤:1被选中—->2被点击。通常这两个步骤先后顺序应该为被点击—–>被选中。但是这样需要对每一个子控件分支中都需要添加是否被选中的判断,代码显得重复。

所以在此我们使用逆向被点击—–>被选中。在被选中这一步中使用一个向上转型是为了可以获取所有CompoundButton子类的状态。如果直接强转为某一具体子类,则不具备通用性,不适应判断所有CompoundButton

子类的被选中状态。

当UI界面中状态切换控件CompoundBuuton与普通Button均存在的情况下,建议对两种控件的使用不同的方法进行监听,例如:android:onClick=”compoundButtonClick”与android:onClick=”buttonClick”

 

二、CompoundButton扩展

—未完待续

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/154810.html原文链接:https://javaforall.net

(0)
上一篇 2022年7月1日 上午8:16
下一篇 2022年7月1日 上午8:16


相关推荐

  • tensorflow 安装问题总结

    tensorflow 安装问题总结tensorflow 安装问题总结

    2022年4月24日
    43
  • WebStorm 格式化代码快捷键

    WebStorm 格式化代码快捷键原文链接:https://kaifazhinan.com/webstorm-formatting-code-shortcuts/现在平时都是使用VSCode作为日常开发工具,偶尔会打开WebStorm临时使用一下,但是却经常忘记格式化代码的快捷键,放在这里防止遗忘。WindowsWindows系统下WebStorm格式化代码的快键键为:Ctrl+Alt+l…

    2022年4月27日
    89
  • break能不能跳出if语句_python while if

    break能不能跳出if语句_python while if广告关闭腾讯云11.11云上盛惠,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元!虽然在python中的for循环与其它语言不大一样,但跳出循环还是与大多数语言一样,可以使用关键字continue跳出本次循环或者break跳出整个for循环。breakforxinrange(10):ifx==5:breakprintx上面使用的break循环,所以执行到x==5…

    2026年4月14日
    3
  • CRC校验算法详解「建议收藏」

    CRC校验算法详解「建议收藏」CRC(CyclicRedundancyCheck)循环冗余校验是常用的数据校验方法,讲CRC算法的文章很多,之所以还要写这篇,是想换一个方法介绍CRC算法,希望能让大家更容易理解CRC算法。先

    2022年8月2日
    15
  • setScale,preScale 和 postScale 的区别

    setScale,preScale 和 postScale 的区别setScale,preScale和postScale的区别上面讲到,Matrix由3*3矩阵中9个值来决定。而我们对Matrix的所有设置,也是对这9个值的各种不同的改变,来达到我们想要的效果。下面是Matrix3*3的矩阵结构{MSCALE_X,MSKEW_X,MTRANS_X,MSKEW_Y,MSCALE_Y,MTRANS_Y,MPERSP_0,MPERSP_1,MPERSP_2}一、首先介绍Scale缩放的控制scale就是缩放,我们调用Matrix的setScale、preSc

    2022年10月20日
    6
  • arcgis python 教程-ArcGIS Python 入门到精通,视频教程下载

    arcgis python 教程-ArcGIS Python 入门到精通,视频教程下载课程介绍:本课程15章42个视频,基于ArcGIS10.2版本,涵盖了如何使用Python开发ArcGIS自定义工具,具体包括:编辑器的使用安装;列表函数使用;汉字乱码处理;游标(cursor)查询、更新和插入;几何图形生成和坐标导出;属性查询和空间查询;字段映射(FieldMappings)和值表(ValueTable)使用;拓扑检查和创建的拓扑处理;文件TXT、XLS和ArcGIS数据转换;使…

    2022年6月26日
    36

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号