[基础控件]—状态切换控件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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • socket网络编程——UDP编程流程「建议收藏」

    socket网络编程——UDP编程流程「建议收藏」UDP提供的是无连接、不可靠的、数据报服务。编程流程如下:socket()方法用来创建套接字,使用udp协议时,选择数据报服务SOCK_DGRAM。sendto()方法用来发送数据,由于UDP是无连接的,每次发送数据都需要指定对端的地址(IP和端口)。recvfrom()方法接收数据,每次都需要传给该方法一个地址结构来存放发送端的地址。recvfrom()方法可以接收所有客户端发送给当前应用程序的数据,并不是只能接收某一个客户端的数据。UDP服务端代码:#include<stdi

    2025年9月4日
    7
  • nacivat错误生成激活码_最新在线免费激活

    (nacivat错误生成激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html1S…

    2022年3月27日
    77
  • 百度UEditor基本使用

    百度UEditor基本使用

    2021年9月18日
    130
  • AspNetPager组件

    AspNetPager组件UrlPaging=”false”NumericButtonTextFormatString=”[{0}]”CustomInfoHTML=”第%CurrentPageIndex%页共%PageCount%页显示%StartRecordIndex%-%EndRecordIndex%条”                                   ShowCusto

    2025年7月31日
    5
  • Pytorch的nn.Conv2d()详解

    Pytorch的nn.Conv2d()详解nn.Conv2d()的使用、形参与隐藏的权重参数in_channelsout_channelskernel_sizestride=1padding=0dilation=1groups=1bias=Truepadding_mode=’zeros’nn.Conv2d()的使用、形参与隐藏的权重参数  二维卷积应该是最常用的卷积方式…

    2022年4月7日
    264
  • 如何使用adb命令安装apk到电视上[通俗易懂]

    如何使用adb命令安装apk到电视上[通俗易懂]使用此命令之前,先确定你的电视已打开adb调试服务如何打开请参考:TCLMS平台电视如何实现adb连接从而安装第三方应用程序需要用到的软件Windows下,选择”开始”-&amp;gt;运行-&amp;gt;cmd,进入命令行模式;进入adb的目录,如adb在D盘adbtools文件夹中,则:d:cdadbtools如果嫌麻烦,可以在adb目录中右击|在此处直接打开命令窗口输…

    2022年5月15日
    180

发表回复

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

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