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

[基础控件]—状态切换控件CompoundButton及其子类CheckBox、RadioButton、ToggleButton、switch事件监听与场景使用…[通俗易懂]一、事件监听对于普通的Button,对其进行事件监听Google官方给出了常见的三种监听方式:1、对每一个button设置事件监听器button.setOnClickListener(View.OnclickListenerlistener);此种方法当button按钮较多时代码显得多、乱、不够简洁明了。2、在Activity中实现接口View.OnclickListener,然后重写…

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

一、事件监听

对于普通的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://www.cnblogs.com/android001/p/4311558.html

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

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Java集合篇:集合细节:为集合指定初始容量、asList的缺陷、subList的缺陷

    Java集合篇:集合细节:为集合指定初始容量、asList的缺陷、subList的缺陷

    2021年10月4日
    39
  • RewriteCond 重写规则执行条件

    RewriteCond 重写规则执行条件RewriteCond重写规则执行条件语法:RewriteCondTestStringCondPattern生效域:serverconfig,virtualhost,directory,.htaccess特别的上面的TestString,可提供反向引用.引用模式为:%N其中N为(0<=N<=9),引用当前若干RewriteCo…

    2022年5月7日
    38
  • if python用法_for循环语句

    if python用法_for循环语句今天,我们将学习Python中if语句的基本使用。if在Python中用作某个条件或值的判断,格式为:if条件: 执行语句1else: 执行语句2else是当条件不成立时运行的代码。我们先来看个例子,程序判断天气情况并输出是否要带伞:weather=input(“今日天气是:”)ifweather==”雨天” print(“今天出门需要带伞”)else: print(“今天出门不需要带伞”)运行代码,输入雨天会提示要带伞。if语句中用的两个“=”是什么呢

    2022年9月26日
    4
  • html中三角向下符号,使用css实现三角符号效果[通俗易懂]

    html中三角向下符号,使用css实现三角符号效果[通俗易懂]关于使用css制作三角符号,网上有很多的例子了,在这里只是为了详细的向各位解释一下三角符号的原理下图,是一个长宽为100px,边框宽度为100px的一个元素,由此可见,在css中上下左右的边框相交处并不是一个直线,所以,可以根据此属性进行编写三角符号那么如何使用css的该属性来实现三角符号的效果呢,代码如下:html代码css代码div:after{position:absolute;width…

    2025年5月28日
    5
  • 媒体查询

    媒体查询

    2021年7月4日
    97
  • 性能优化之YUICompressor压缩JS、CSS

    性能优化之YUICompressor压缩JS、CSS性能一直是项目中比较重要的一点,尤其门户网站,对页面的响应要求是很高的,从性能角度上来讲,对于Web端的优化其中重要的一点无疑是JS、CSS文件压缩,图片的融合,尽量减小文件的大小,必免占加载时占用过多的带宽。yuicompressor无疑是一个比较好的压缩工具,是yahoo的一个开源组件,下面介绍yuicompressor压缩JS、CSS文件,及在项目中的使用yuicmpressor的使用1、首先

    2022年7月18日
    21

发表回复

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

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