Android widget之CompoundButton[通俗易懂]

Android widget之CompoundButton[通俗易懂]ButtonCheckBoxRadioButtonSwitchSwitchCompatToggleButtonAppCompatCheckBoxAppCompatRadioButton

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

简介

具有两个状态的按钮,已选中或未选中。当按下或点击按钮时,状态会自动更改。

  • 直接继承至Button
  • 直接子类
    1. CheckBox
    2. RadioButton
    3. Switch
    4. SwitchCompat
    5. ToggleButton
  • 间接子类
    1. AppCompatCheckBox
    2. AppCompatRadioButton

使用

相比较Button而言多出了一个监听事件(接口)

CompoundButton.OnCheckedChangeListener

当复合按钮的检查状态发生变化时调用。

实现方法:onCheckedChanged( CompoundButton buttonView,boolean isChecked)

  • buttonView 复合按钮视图的状态。
  • isChecked buttonView的新状态。

公共方法

简单介绍几个常用的

  • isChecked() — 获取当前状态
  • performClick() — 调用此视图的OnClickListener(如果已定义)
  • setChecked(boolean checked) — 更改这个按钮的状态
  • setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener listener)
    当这个按钮的检查状态发生变化时,注册一个回调
  • toggle() — 将视图的状态更改为当前状态的逆(反向)

子类

CheckBox

复选框:可以选中或取消选中的特定类型的双状态按钮。

例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
    <CheckBox android:id="@+id/checkbox_meat" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/meat" android:onClick="onCheckboxClicked"/>
    <CheckBox android:id="@+id/checkbox_cheese" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/cheese" android:onClick="onCheckboxClicked"/>
</LinearLayout>
public void onCheckboxClicked(View view) {
    // Is the view now checked?
    boolean checked = ((CheckBox) view).isChecked();

    // Check which checkbox was clicked
    switch(view.getId()) {
        case R.id.checkbox_meat:
            if (checked)
                // Put some meat on the sandwich
            else
                // Remove the meat
            break;
        case R.id.checkbox_cheese:
            if (checked)
                // Cheese me
            else
                // I'm lactose intolerant
            break;
        // TODO: Veggie sandwich
    }
}
public class MyActivity extends Activity { 
   
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.content_layout_id);
         final CheckBox checkBox = (CheckBox) findViewById(R.id.checkbox_id);
         if (checkBox.isChecked()) {
             checkBox.setChecked(false);
         }
     }
 }

注:AppCompatCheckBox作为其子类用法差别不大!

RadioButton

单选按钮:是可以选中或取消选中的双状态按钮。当单选按钮被取消选中时,用户可以单击来选中它。

  • 注:单选按钮通常与RadioGroup在一起使用。当多个单选按钮在RadioGroup内时,检查一个单选按钮将取消选中所有其他单选按钮。
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical">
    <RadioButton android:id="@+id/radio_pirates" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/pirates" android:onClick="onRadioButtonClicked"/>
    <RadioButton android:id="@+id/radio_ninjas" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/ninjas" android:onClick="onRadioButtonClicked"/>
</RadioGroup>
public void onRadioButtonClicked(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.radio_pirates:
            if (checked)
                // Pirates are the best
            break;
        case R.id.radio_ninjas:
            if (checked)
                // Ninjas rule
            break;
    }
}

注:AppCompatRadioButton作为其子类用法差别不大!

Switch

开关:是一个双状态切换开关小部件,可以在两个选项之间进行选择。用户可以来回拖动“拇指”来选择所选择的选项,或者只需轻按以切换,就像复选框一样。该text 属性控制交换机标签中显示的文本,而 文本off和on文本控制拇指上的文本。

xml属性 公共方法 作用效果
android:showText setShowText(boolean) 是否显示 打开/关闭 文本
android:textOff setTextOff(CharSequence) 当开关处于 关闭 状态时使用的文本
android:textOn setTextOn(CharSequence) 当开关在 开打 状态时使用的文本
android:track setTrackResource(int) 开关拇指滑动的“轨迹”

ToggleButton

显示 打开/关闭 的状态的按钮,默认情况下伴随文本“ON”或“OFF”。

与Switch差别不大!

知识不够用?

http://android.xsoftlab.net/reference/android/widget/CompoundButton.html


知识贵在分享!

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

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

(0)
上一篇 2022年6月7日 下午10:36
下一篇 2022年6月7日 下午10:46


相关推荐

  • 安卓支付宝抢红包脚本软件

    安卓支付宝抢红包脚本软件软件使用条件 1 安卓手机 2 已经 ROOT3 没有了 ps 软件可能会提示不支持你的分辨率请无视 使用方法 1 下载软件打开 小米手机可能要设置下悬浮窗 2 给予软件 root 权限 3 点启动 4 打开支付宝等到倒计时结束马上按音量 键 5 抢红包结束马上按音量 ps 由于用的是找色代码结束没及时按音量 软件会乱点

    2026年3月19日
    2
  • Windows10系统设置定时关机「建议收藏」

    Windows10系统设置定时关机「建议收藏」1.输入cmd运行命令提示符2.输入 shutdown–s–t3600   [注:3600为倒计时的秒数,此语句为系统在一小时后关机]/*shutdown命令的参数:shutdown-a 取消关机shutdown-s关机shutdown-f 强行关闭应用程序shutdown-m计算机名 控制远程计算机shutdown-i 显示“远程关机”图形用户界面,…

    2022年5月15日
    44
  • 贪吃蛇的程序代码_贪吃蛇代码怎么运行

    贪吃蛇的程序代码_贪吃蛇代码怎么运行效果按方向键移动按空格加速按esc暂停按两次esc退出随着蛇的长度增加,蛇移动速度加快没有屏闪main()intmain(){ Init(); //初始化 while(!die) { ProcessKey(); //处理按键 Move(); //移动蛇 Judge(); //碰撞检测 Draw(); /…

    2025年9月13日
    7
  • Rest风格的请求方式

    Rest风格的请求方式关注作者更多博客 一起学习一起进步 Spring 自定义注解学习 https blog csdn net article details JVM 底层原理学习 https blog csdn net article details JAVA 多线程学习 https blog csdn net 34

    2026年3月19日
    1
  • iOS版本比较方法

    之前一直把版本号转换为floatValue,但是最近在项目中又出现了float的问题,主要是NSString表示为@"17.30",转换为floatValue值为17.2999

    2021年12月25日
    79
  • awvs使用教程_awm20706参数

    awvs使用教程_awm20706参数目录:0×00、什么是AcunetixWebVulnarabilityScanner(WhatisAWVS?)0×01、AWVS安装过程、主要文件介绍、界面简介、主要操作区域简介(InstallAWVSandGUIDescription)0×02、AWVS的菜单栏、工具栏简介(AWVSmenubar&amp;toolsbar)0×03、开始一次…

    2026年2月19日
    6

发表回复

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

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