Android 中CheckBox复选框按钮的基本用法

博主前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住也分享一下给大家
👉点击跳转到教程

效果图:
在这里插入图片描述
activity_checkbox.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"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
    <TextView
        android:id="@+id/tv_hobby"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我的爱好:"
        android:textSize="20sp"
        />
    <CheckBox
        android:id="@+id/ck_book"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="读书"
        />
    <CheckBox
        android:id="@+id/ck_sport"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="运动"
        />
    <CheckBox
        android:id="@+id/ck_travel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="旅游"
        />
    </LinearLayout>
    <Button
        android:id="@+id/btn_commit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="提交"
        />
</LinearLayout>

CheckboxActivity代码:

public class CheckboxActivity extends AppCompatActivity implements View.OnClickListener {
    private CheckBox ck_book,ck_sport,ck_travel;
    private Button btn_commit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_checkbox);
        ck_book =findViewById(R.id.ck_book);
        ck_sport =findViewById(R.id.ck_sport);
        ck_travel =findViewById(R.id.ck_travel);

        btn_commit = findViewById(R.id.btn_commit);
        btn_commit.setOnClickListener(this);
		//给复选框添加选中状态改变的监听器
        ck_book.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            	//如果选中则获取选中的值
                if (ck_book.isChecked()){
                    Toast.makeText(CheckboxActivity.this, "我的爱好:"+ck_book.getText(), Toast.LENGTH_SHORT).show();
                }
            }
        });
        ck_sport.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (ck_sport.isChecked()){
                    Toast.makeText(CheckboxActivity.this, "我的爱好:"+ck_sport.getText(), Toast.LENGTH_SHORT).show();
                }
            }
        });
        ck_travel.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (ck_travel.isChecked()){
                    Toast.makeText(CheckboxActivity.this, "我的爱好:"+ck_travel.getText(), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    //获取复选框选中的值
    @Override
    public void onClick(View v) {
        //定义一个可变字符序列 用来存储复选框选中的值
        StringBuilder builder = new StringBuilder();
        if (ck_book.isChecked()) {
            builder.append(ck_book.getText()+",");
        }
        if (ck_sport.isChecked()) {
            builder.append(ck_sport.getText()+",");
        }
        if (ck_travel.isChecked()) {
            builder.append(ck_travel.getText()+",");
        }
        Toast.makeText(this, "我的爱好为:"+builder, Toast.LENGTH_LONG).show();
    }
}
  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

路宇

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

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

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

打赏作者

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

抵扣说明:

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

余额充值