Android基础控件CheckBox的使用

1.简单介绍

CheckBox有两种状态,勾选和未勾选,是一个很简单的控件。可以通过实现OnCheckedChangeListener接口来监听CheckBox;如果想更改它的图标的话只需更改它的button属性即可实现。
CheckBox

2.简单实现

下面是使用CheckBox的示例代码。

  1. check_box_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<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>
  1. activity_check_box.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CheckBoxActivity"
    android:orientation="vertical">

    <!--系统自带图标的CheckBox-->
    <CheckBox
        android:id="@+id/ck_check_box_demo1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="这是一个系统自带图标的CheckBox"
        android:textSize="18sp"/>
    
    <!--自定义图标的CheckBox-->
    <CheckBox
        android:id="@+id/ck_check_box_demo2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="这是一个改了图标的CheckBox"
        android:textSize="18sp"
        android:button="@drawable/check_box_selector"/>
</LinearLayout>
  1. CheckBoxActivity.java
package xyz.strasae.androidlearn.my;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

public class CheckBoxActivity extends AppCompatActivity {
    CheckBox checkBoxDemo1;
    CheckBox checkBoxDemo2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_box);
        checkBoxDemo1 = findViewById(R.id.ck_check_box_demo1);
        checkBoxDemo1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                Toast.makeText(CheckBoxActivity.this, String.format("你碰了了控件%s,状态为%b", compoundButton.getText().toString(), b), Toast.LENGTH_SHORT).show();
            }
        });
        checkBoxDemo2 = findViewById(R.id.ck_check_box_demo2);
        checkBoxDemo2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                Toast.makeText(CheckBoxActivity.this, String.format("你碰了了控件%s,状态为%b", compoundButton.getText().toString(), b), Toast.LENGTH_SHORT).show();
            }
        });
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值