<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--
android:checked = "设置初选状态"
-->
<CheckBox
android:id="@+id/checkBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CheckBox"
android:checked="true"/>
</LinearLayout>
--------------------------------------------------分界线-------------------------------------------------------
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.CheckBox; import android.widget.CompoundButton; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_checkbox); CheckBox checkBox = findViewById(R.id.checkBox); //设置是否选中 checkBox.setChecked(false); //判断是否选中 boolean isChecked = checkBox.isChecked(); //监听控件状态 checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { //选中与否都可以进行下一步操作 } }); } }