Widget simple component radio buttons and radio button groups

Overview of radio buttons and radio button groups

There is one and only one button in a group of buttons that can be selected. When a button in the button group is selected, the selected state of other buttons will be cancelled. The above effects need to be used in conjunction with RadioButton and RadioGroup to achieve. RadioGroup is a radio button group, a container that allows multiple RadioButtons. In the absence of RadioGroup, RadioButton can be selected separately; when multiple RadioButtons are in the same RadioGroup button group, RadioButton only allows one of them to be selected. In different RadioGroup, RadioButton does not affect each other.

The RadioGroup class is a subclass of LinearLayout, and its commonly used methods for setting and controlling radio button groups are shown in the following figure

RadioButton related methods

method Function description
getCheckedRadioButtonId() Get the ID of the selected button
clearCheck() Clear selected state
check(int id) Set the option to the selected state by parameter D; if a 1 is passed in, the check state of the radio button group is cleared, which is equivalent to calling the clearCheck() operation
setOnCheckedChangeListener (RadioGroup.OnCheckedChangeListenerlistener) In a radio button group, the callback function to be called when the check state of the radio button changes. When the checked property of RadioButton is true, the check(id) method will not trigger the onCheckedChanged event
addView ( View child, int index,ViewGroup.LayoutParams params) Add a subview using the specified layout parameters. Among them: child is the sub-view to be added; index is the position where the sub-view will be added; params is the layout parameter of the sub-view to be added
getText() Used to get the value of the radio button

Through the OnCheckedChangeListener listener, the state switching of the radio buttons can be monitored and processed.

Code demo

The following is a simple example to demonstrate the use of RadioButton and RadioGroup

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="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:orientation="vertical">
    <!-- 显示选择的内容 1 -->
    <TextView
        android:id="@+id/chooseTxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我选择的是...?"
        android:textSize="30sp" />
    <!-- 单选按钮组 2 -->
    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="按钮1"/>
        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="按钮2"/>
    </RadioGroup>
    <!--清除所有选中的状态 3 -->
    <Button
        android:id="@+id/radio_clearBtn"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="清除选中"/>
    <!-- 往按钮组中添加新的单选按钮 4 -->
    <Button
        android:id="@+id/radio_addBtn"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="添加子项"/>
</LinearLayout>

The code is explained as follows: label 1 is used to display the title of the currently selected button; label 2 defines a radio button group, and adds two radio buttons to the button group; label 3 defines a "clear" button It is used to clear the selected state of all radio buttons in the button group; label 4 defines an "add child" button, which is used to add new mutually exclusive radio buttons to the button group.

Next, demonstrate the use of the button group in the corresponding Activity to realize the function of clearing the selected state of all buttons in the button group and adding new radio buttons to the button group
Insert picture description hereInsert picture description here

code show as below

package com.qst.demo2;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class RadioButtonActivity extends AppCompatActivity {
    
    
    private TextView chooseTxt;   //显示选择的单选按钮文本 1
    private RadioGroup radioGroup;  //按钮组
    //多个单选按钮
    private RadioButton radioButton1;
    private RadioButton radioButton2;
    private Button radioClearBtn; //清除按钮
    private Button radioAddBtn;   //新增按钮
    @Override   //重写父类的onCreatefangfa
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);  //调用父类onCreate方法创建Activity
        setContentView(R.layout.radiobutton_demo);   //设置布局为radiobutton_demo
        //初始化按钮 2
        chooseTxt = (TextView) findViewById(R.id.chooseTxt);
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        radioButton1 = (RadioButton) findViewById(R.id.radioButton1);
        radioButton2 = (RadioButton) findViewById(R.id.radioButton2);
        radioGroup.setOnCheckedChangeListener(onCheckedChangeListener); //设置监听器
        //清除选中状态
        radioClearBtn = (Button) findViewById(R.id.radio_clearBtn);   //初始化
        radioClearBtn.setOnClickListener(onClickListener);  //设置监听器
        //增加子选项
        radioAddBtn = (Button) findViewById(R.id.radio_addBtn); //初始化
        radioAddBtn.setOnClickListener(onClickListener);  //设置监听器
    }
    /** 定义按钮组的监听事件 3 */
    private RadioGroup.OnCheckedChangeListener onCheckedChangeListener  //创建对象
            = new RadioGroup.OnCheckedChangeListener() {
    
      //使用内部类的监听器
        @Override   //重写方法
        public void onCheckedChanged(RadioGroup group, int checkedId) {
    
    
              //获取当前选中的按钮的Id
            switch (radioGroup.getCheckedRadioButtonId()) {
    
    
                case R.id.radioButton1:
                    chooseTxt.setText("我选择的是:" + radioButton1.getText());
                    break;
                case R.id.radioButton2:
                    chooseTxt.setText("我选择的是:" + radioButton2.getText());
                    break;
                default:
                    chooseTxt.setText("我选择的是: 新增" );
                    break;
                    }
        }
    };
    //定义清除状态按钮和新增按钮的单击事件 4
    private View.OnClickListener onClickListener = new View.OnClickListener() {
    
    //使用外部类的监听器
        @Override  //重写父类方法
        public void onClick(View view) {
    
    
            switch (view.getId()) {
    
    
                case R.id.radio_clearBtn:
                    radioGroup.check(-1);
                    chooseTxt.setText("我选择的是...?");
                    break;
                case R.id.radio_addBtn:
                    //新增子选项
                    RadioButton newRadio = new RadioButton(RadioButtonActivity.this);
                    newRadio.setLayoutParams(new ViewGroup.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.MATCH_PARENT));
                    newRadio.setText("新增");
                    radioGroup.addView(newRadio, radioGroup.getChildCount());
                    break;
                default:
                    break;
            }
        }
    };
}

The code is explained as follows: label 1 defines an attribute variable chooseTxt of type TextView, which is used to obtain the text of the currently selected button and the definition type of other attributes; label 2 initializes each attribute variable defined at label 1 by The assignment of attribute variables makes it possible for subsequent business logic operations; a button group listener object is defined at label 3, which is used to obtain the currently selected radio button object in the button group, and display the text on the chooseTxt object ; The label ④ defines a common button listener object, which is used to realize the business logic functions of radioClearBtn and radioAddBtn. When the user clicks the radioClearBtn button, the selected radio button state in the button group is cleared; when the user clicks radioAddBtn , The system will add a radio button object to the radioGroup object.

Declare Activity in the AndroidMainfest.xml manifest file
Insert picture description here

operation result
Insert picture description here

Guess you like

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