Android Studio 复选框CheckBox

功能

复选框用于选择某几项内容,经典场景如选课,从语文、数学、英语中选择1门或者2门。

显示

在布局文件中,注意通过checked属性设置是否默认选中即可,代码如下:

<?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:padding="4dp">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="请选择课程"/>
    <CheckBox
        android:id="@+id/checkBoxChinese"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="语文"/>
    <CheckBox
        android:id="@+id/checkBoxMath"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="数学"/>
    <CheckBox
        android:id="@+id/checkBoxEnglish"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="英语"/>
    <Button
        android:id="@+id/buttonOk"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="确认"/>
</LinearLayout>

效果如下:
在这里插入图片描述

获取选中项

点击确认后,后台获取选中项,并弹窗显示选中项。

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取按钮
        Button buttonOk = findViewById(R.id.buttonOk);
        //设置按钮点击监听器
        buttonOk.setOnClickListener(new MyOnClickListener());
    }
    //定义按钮点击监听器
    class MyOnClickListener implements View.OnClickListener {
        //按钮点击
        @Override
        public void onClick(View view) {
            if (view.getId() == R.id.buttonOk) {//被点击的是确认按钮
                //获取选中项
                CheckBox checkBoxChinese=findViewById(R.id.checkBoxChinese);
                CheckBox checkBoxMath=findViewById(R.id.checkBoxMath);
                CheckBox checkBoxEnglish=findViewById(R.id.checkBoxEnglish);
                StringBuilder sb=new StringBuilder();
                if(checkBoxChinese.isChecked()==true){
                    sb.append("语文;");
                }
                if(checkBoxMath.isChecked()==true){
                    sb.append("数学;");
                }
                if(checkBoxEnglish.isChecked()==true){
                    sb.append("英语;");
                }
                //显示提示框
                Toast.makeText(MainActivity.this, "你选择了:"+sb.toString() , Toast.LENGTH_SHORT).show();
            }
        }
    }
}

在这里插入图片描述

监听选中项变化

当选中项变化时,提示用户选择信息,代码如下:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Switch switchMsg = findViewById(R.id.switchMsg);
        switchMsg.setOnCheckedChangeListener(new MyOnCheckedChangeListener());
    }
    //定义按钮点击监听器
    class MyOnCheckedChangeListener implements CompoundButton.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            String str = "";
            if (b == true) {
                str = "开启";
            } else {
                str = "关闭";
            }
            //显示提示框
            Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章