android 复选框CheckBox

compoundButton(复合按钮)继承与Button,拥有Button的属性和方法。

compoundButton有:CheckBox , RadioButton,Switch。

compoundButton在Java代码中主要使用4中方法:

setChecked:设置按钮的勾选状态。

setButtonDrawable:设置左侧勾选图标的图形资源。

setOnCheckChangeListener:设置勾选状态变化的监视器。

isChecked:判断按钮是否勾选。

复选框CheckBox是CompoundButton一个最简单的实现控件,点击复选框将它勾选,再次点击取消勾 选。复选框对象调用setOnCheckedChangeListener方法设置勾选监听器,这样在勾选和取消勾选时就 会触发监听器的勾选事件。

案例:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true"
        android:drawable="@drawable/check_choose"/>
    <item android:drawable="@drawable/check_unchoose"/>
</selector>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp">
    <CheckBox
        android:id="@+id/ck_system"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:checked="false"
        android:text="这是系统的checkbox" />
    <CheckBox
        android:id="@+id/ck_custom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
<!--        改变左边的图标用button属性-->             android:button="@drawable/checkbox_selector4"
        android:checked="true"
        android:padding="5dp"
        android:text="这个CheckBox换了图标" />
</LinearLayout>
CheckBox ck_system = findViewById(R.id.ck_system);
CheckBox ck_custom = findViewById(R.id.ck_custom);
// ck_system,ck_custom设置了监听器,一旦出发就执行onCheckedChanged方法
ck_system.setOnCheckedChangeListener(this);
ck_custom.setOnCheckedChangeListener(this);
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    String desc = String.format("您%s了这个CheckBox",isChecked ? "勾选":"取消");
    buttonView.setText(desc);
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Android 复选框组件是一种可以允许用户选择一个或多个选项的 UI 控件。它通常呈现为一个小方框,里面包含一个勾选标记。用户可以通过点击方框来选择或取消选择选项。 在 Android 中,复选框组件是 CheckBox 类。除了基本的选择和取消选择之外,CheckBox 还提供了一些其他的功能,例如设置复选框的文本标签、设置复选框的状态(选中、未选中、不确定)、设置复选框的背景等。 以下是一个简单的示例,演示如何在 Android 应用程序中使用复选框组件: ```xml <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选项1" /> <CheckBox android:id="@+id/checkBox2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选项2" /> <CheckBox android:id="@+id/checkBox3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选项3" /> ``` 在代码中,可以使用 CheckBox 的 isChecked() 方法来检查复选框是否被选中。例如,以下代码将显示一个 Toast 消息,指示哪些复选框选中: ```java CheckBox checkBox1 = findViewById(R.id.checkBox1); CheckBox checkBox2 = findViewById(R.id.checkBox2); CheckBox checkBox3 = findViewById(R.id.checkBox3); String message = "选中的选项:"; if (checkBox1.isChecked()) { message += "选项1 "; } if (checkBox2.isChecked()) { message += "选项2 "; } if (checkBox3.isChecked()) { message += "选项3 "; } Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); ``` 总的来说,CheckBox 是一个非常有用的 Android UI 控件,可以让用户方便地选择多个选项。它的使用方法很简单,只需要在布局文件中添加 CheckBox,然后在代码中检查它们的状态即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

当当2000

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值