Widget simple component CheckBox check box

Article Directory

CheckBox checkbox

The CheckBox check box is a double-state button, which has two states: checked or unchecked. When defining the check button in the layout file, register the OnCheckedChangeListener event listener for each button, and then judge whether the option is selected according to the isChecked parameter in the onCheckedChanged() event processing method.
The main differences between CheckBox and RadioButton are as follows:

  • After the RadioButton radio button is selected, its state cannot be changed when clicked again, while the CheckBox checkbox can be clicked to change its state.
  • In the RadioButton radio button group, only one is allowed to be selected; while in the CheckBox checkbox, multiple selections are allowed at the same time.
  • In most UI frames, RadioButton is represented by a circle by default, and CheckBox is represented by a rectangle by default.

Code demo

Let’s use a simple example to demonstrate the use of CheckBox. Taking multiple choices of "sports hobbies" as an example, people’s "sports hobbies" may include football, basketball, etc., but people’s gender choices are different, and they can only choose "male". "Or "female", and the two are mutually exclusive.

Insert picture description here
Insert picture description here
The layout code is as follows

<?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:layout_gravity="center_horizontal"
    android:orientation="vertical">
    <!-- 基本显示 1 -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/title"
        android:textSize="20sp"
        android:textStyle="bold"
        />
    <!--足球 2 -->
    <CheckBox
        android:id="@+id/checkbox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/football"
        android:textSize="16sp"
        />
    <!-- 篮球 3 -->
    <CheckBox
        android:id="@+id/checkbox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/basketball"
        android:textSize="16sp"
        />
    <!-- 排球 4 -->
    <CheckBox
        android:id="@+id/checkbox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/volleyball"
        android:textSize="16sp"
        />
</LinearLayout>

The code is explained as follows: the TextView at label 1 is used to display the user's title; the label 2 defines the "football" check box; the label 3 defines the "basketball" check box; the label 4 defines "volleyball" "Checkbox.

In the above code, the text part of the check box uses string resources, for example, the text of "football" is the string in the quoted strings.xml file

The string definition in strings.xml is as follows

<resources>
    <string name="title">你喜欢的运动是:</string>
    <string name="app_name">复选框测试</string>
    <string name="football">足球</string>
    <string name="basketball">篮球</string>
    <string name="volleyball">排球</string>
</resources>

The purpose of using the strings.xml file in the development process is as follows

  • For internationalization. Android recommends that the text displayed on the screen be defined in strings.xml. If you need to internationalize in the future, you only need to modify the string.xml file.
  • In order to reduce the volume of the application and reduce the redundancy of the data.

The following demonstrates the use of check boxes in the corresponding Activity. When the user selects different "hobbies", the user's selection results are displayed on the screen

Insert picture description here
Insert picture description here

code show as below

package com.qst.demo2;

import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class CheckBoxDemoActivity extends AppCompatActivity {
    
    
    //声明复选框 1
    private CheckBox footballChx;
    private CheckBox basketballChx;
    private CheckBox volleyballChx;
    @Override  //重写父类的onCreate方法
    public void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState); //调用父类的onCreate方法创建Activity
        setContentView(R.layout.checkbox_demo);  //设置布局
        //通过findViewById获得CheckBox对象 2
        footballChx = (CheckBox) findViewById(R.id.checkbox1);
        basketballChx = (CheckBox) findViewById(R.id.checkbox2);
        volleyballChx = (CheckBox) findViewById(R.id.checkbox3);
        //注册事件监听器 3
        footballChx.setOnCheckedChangeListener(listener);
        basketballChx.setOnCheckedChangeListener(listener);
        volleyballChx.setOnCheckedChangeListener(listener);

    }
    //使用内部类形式的响应事件 4
    private CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
    
    
        @Override //重写接口的方法
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    
    
            switch (buttonView.getId()) {
    
    
                case R.id.checkbox1:
                    //选择足球
                    if (isChecked) {
    
    
                        //Toast的使用 5
                        Toast.makeText(CheckBoxDemoActivity.this, "你喜欢足球",
                                Toast.LENGTH_LONG).show();
                    }
                    break;
                case R.id.checkbox2:
                    //选择篮球
                    if (isChecked) {
    
    
                        Toast.makeText(CheckBoxDemoActivity.this, "你喜欢篮球",
                                Toast.LENGTH_LONG).show();
                    }
                    break;
                case R.id.checkbox3:
                    //选择排球
                    if (isChecked) {
    
    
                        Toast.makeText(CheckBoxDemoActivity.this, "你喜欢排球",
                                Toast.LENGTH_LONG).show();
                    }
                    break;
                default:
                    break;
            }
        }
    };
}

The code is explained as follows: 3 CheckBox checkboxes are defined in label 1 for users to choose; label 2 is initialized to each attribute variable defined in label 1, and the attribute variable is assigned to make it possible for subsequent business logic Operation; label 3 sets up a listener for the three CheckBox objects to monitor their respective selection or cancellation events; label 4 defines a listener object to monitor and implement the business logic functions of the three CheckBoxes, when the user When you click a different CheckBox, the corresponding text information will be displayed through the Toast object on the screen. Toast is a mechanism used to display prompt information in Android. The difference from Dialog is: Toast prompt has no focus and time is limited, and it will automatically disappear after a certain period of time.

**Declare Activity in the AndroidMainfest.xml manifest file**

Insert picture description here

Insert picture description here
operation result

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_42768634/article/details/115266817