Android 第十一章 CheckBox

public class MainActivity extends AppCompatActivity {

    private CheckBox cb_1, cb_2, cb_3, cb_4, cb_5, cb_6;
    private Button btnConfirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        initClick();
    }

    private void initView() {
        cb_1 = findViewById(R.id.cb_1);
        cb_2 = findViewById(R.id.cb_2);
        cb_3 = findViewById(R.id.cb_3);
        cb_4 = findViewById(R.id.cb_4);
        cb_5 = findViewById(R.id.cb_5);
        cb_6 = findViewById(R.id.cb_6);
        btnConfirm = findViewById(R.id.btn_confirm);
    }

    private void initData() {
        cb_1.setChecked(true);
    }

    private void initClick() {
        btnConfirm.setOnClickListener(v -> {
            if (cb_1.isChecked() && !cb_2.isChecked() && cb_3.isChecked() && cb_4.isChecked() && cb_5.isChecked() && !cb_6.isChecked()) {
                Toast.makeText(MainActivity.this, "恭喜回答正确!", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(MainActivity.this, "回答错误!", Toast.LENGTH_LONG).show();
            }
        });
    }
}
<?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"
    android:orientation="vertical"
    android:padding="30dp"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="天有天眼;地有地窟;河里漂带;沟里褪壳。打4个东西"
        android:textColor="@color/black"
        android:textSize="30sp" />

    <CheckBox
        android:id="@+id/cb_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:text="星星"
        android:textSize="20sp" />

    <CheckBox
        android:id="@+id/cb_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="月亮"
        android:textSize="20sp" />

    <CheckBox
        android:id="@+id/cb_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="20sp" />

    <CheckBox
        android:id="@+id/cb_4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="海带"
        android:textSize="20sp" />

    <CheckBox
        android:id="@+id/cb_5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="龙虾"
        android:textSize="20sp" />

    <CheckBox
        android:id="@+id/cb_6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="螃蟹"
        android:textSize="20sp" />

    <Button
        android:id="@+id/btn_confirm"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:text="确认" />


</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值