Android——复选框CheckBox

本文详细介绍了Android中的复选框CheckBox,包括常用属性如自定义样式android:button,通过引用drawable资源来改变按钮外观。同时,探讨了如何设置事件监听,以实现交互功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

常用属性

 <CheckBox android:id="@+id/cb_1"
            android:layout_width="wrap_content"
              android:layout_height="wrap_content"
               android:text="足球"
              android:textSize="20dp"
              android:layout_below="@+id/text"
              android:layout_marginBottom="10dp"
    />
    <CheckBox android:id="@+id/cb_2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="篮球"
              android:textSize="20dp"
              android:layout_below="@+id/text"
              android:layout_toRightOf="@+id/cb_1"
              android:layout_marginBottom="10dp"
    />
    <CheckBox android:id="@+id/cb_3"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="排球"
              android:textSize="20dp"
              android:layout_toRightOf="@+id/cb_2"
              android:layout_below="@+id/text"
              android:layout_marginRight="@+id/cb_2"
              android:layout_marginBottom="10dp"
    />
    <CheckBox android:id="@+id/cb_4"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="网球"
              android:textSize="20dp"
              android:layout_below="@+id/text"
              android:layout_marginRight="@+id/cb_3"
              android:layout_toRightOf="@+id/cb_3"
              android:layout_marginBottom="10dp"
    />

自定义样式

在这里插入图片描述
android:button="@drawable/bg_button"引用样式

监听事件

import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class CheckBoxActivity extends AppCompatActivity {

    private CheckBox cb1,cb2,cb3,cb4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_box);
        cb1=findViewById(R.id.cb_1);
        cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(CheckBoxActivity.this,isChecked?"1选中":"1未选中",Toast.LENGTH_SHORT).show();
            }
        });
        cb2=findViewById(R.id.cb_2);
        cb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(CheckBoxActivity.this,isChecked?"2选中":"2未选中",Toast.LENGTH_SHORT).show();
            }
        });
        cb3=findViewById(R.id.cb_3);
        cb3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(CheckBoxActivity.this,isChecked?"3选中":"3未选中",Toast.LENGTH_SHORT).show();
            }
        });
        cb4=findViewById(R.id.cb_4);
        cb4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(CheckBoxActivity.this,isChecked?"4选中":"4未选中",Toast.LENGTH_SHORT).show();
            }
        });

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值