The use of RadioGroup and RadioButton in Android, set the RadioButton selected and click again to deselect.

Recently, BOSS mentioned a requirement. After selecting one in the radio box, click again to cancel the radio. We all know that the radio box is a combination of RadioGroup and RadioButton in Android, but once a RadioButton in a RadioGroup is selected, one must be selected, and it cannot be canceled without special treatment. I figured it out and implemented it in a way, and recorded it in case it will be used later.Tongue out

First look at the xml code. There are 4 RadioButtons in a RadioGroup, and a TextView below is used to display the selected result:

<?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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70dp">
        <RadioGroup
            android:id="@+id/rg_st"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <RadioButton
                android:background="@drawable/shape_frame"
                android:theme="@style/MyRadioButton"
                android:layout_margin="10dp"
                android:gravity="center"
                android:text="孙悟空"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent" />
            <RadioButton
                android:background="@drawable/shape_frame"
                android:theme="@style/MyRadioButton"
                android:layout_margin="10dp"
                android:gravity="center"
                android:text="猪八戒"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent" />
            <RadioButton
                android:background="@drawable/shape_frame"
                android:theme="@style/MyRadioButton"
                android:layout_margin="10dp"
                android:gravity="center"
                android:text="沙悟净"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent" />
            <RadioButton
                android:background="@drawable/shape_frame"
                android:theme="@style/MyRadioButton"
                android:layout_margin="10dp"
                android:gravity="center"
                android:text="白龙马"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent" />
        </RadioGroup>
    </LinearLayout>

    <TextView
        android:gravity="center"
        android:id="@+id/tv_result"
        android:layout_width="match_parent"
        android:layout_height="100dp" />

</LinearLayout>
Looking at the Java code, we generally deal with it this way:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        initView();
    }

    private void initView() {
        mRgST = (RadioGroup) findViewById(R.id.rg_st);
        mTvResult = (TextView) findViewById(R.id.tv_result);

        mRgST.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
        //找到点击的RadioButton
        RadioButton radioButton = (RadioButton) findViewById(i);
        switch (radioGroup.getId()){
            case R.id.rg_st:
                //把RadioButton的文字展示到结果区
                mTvResult.setText(radioButton.getText());
                break;
        }
    }

Look at Ruhe again, select it and click again to deselect it:

    private RadioGroup mRgST;
    private TextView mTvResult;
    /**
     * 定义变量记录选中adioButton的索引
     */
    private int position = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        initView();
    }

    private void initView() {
        mRgST = (RadioGroup) findViewById(R.id.rg_st);
        mTvResult = (TextView) findViewById(R.id.tv_result);

        setRadioGroupCheckedListener(mRgST);
//        mRgST.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
        //找到点击的RadioButton
        RadioButton radioButton = (RadioButton) findViewById(i);
        switch (radioGroup.getId()){
            case R.id.rg_st:
                //把RadioButton的文字展示到结果区
                mTvResult.setText(radioButton.getText());
                break;
        }
    }

    /**
     * 监听
     * @param radioGroup
     */
    private void setRadioGroupCheckedListener(final RadioGroup radioGroup) {
        //遍历RadioGroup中的所有子View也就是RadioButton
        for (int i = 0; i < radioGroup.getChildCount(); i++) {
            final RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
            final int finalI = i;
            //监听RadioButton的点击事件
            radioButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //以下逻辑是在RadioButton点击后(也就是RadioButton状态改变后)才执行
                    //清空RadioGroup的选中状态
                    radioGroup.clearCheck();
                    //记录选中按钮文字的变量
                    String result = "";
                    //判断记录的值是否和点击的索引值是否相同
                    if (position == finalI){//相同:点击的是同一个RadioButton执行以下逻辑
                        radioButton.setChecked(false);//设置RadioButton选中状态为false
                        position = -1;//初始化position的值
                        result = "";//初始化result的值
                    }else {//不同:点击的是不同RadioButton
                        radioButton.setChecked(true);////设置RadioButton选中状态为true
                        position = finalI;//为position赋值
                        result = (String) radioButton.getText();//为result赋值
                    }
                    mTvResult.setText(result);//状态改变后把结果显示到结果区
                }
            });
        }
    }


The above is the operation when there is only one RadioGroup in an Activity. If there are multiple Ruhe processing, it is actually very simple. Looking at xml is actually copying the above one:

<?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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70dp">
        <RadioGroup
            android:id="@+id/rg_st"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <RadioButton
                android:background="@drawable/shape_frame"
                android:theme="@style/MyRadioButton"
                android:layout_margin="10dp"
                android:gravity="center"
                android:text="孙悟空"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent" />
            <RadioButton
                android:background="@drawable/shape_frame"
                android:theme="@style/MyRadioButton"
                android:layout_margin="10dp"
                android:gravity="center"
                android:text="猪八戒"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent" />
            <RadioButton
                android:background="@drawable/shape_frame"
                android:theme="@style/MyRadioButton"
                android:layout_margin="10dp"
                android:gravity="center"
                android:text="沙悟净"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent" />
            <RadioButton
                android:background="@drawable/shape_frame"
                android:theme="@style/MyRadioButton"
                android:layout_margin="10dp"
                android:gravity="center"
                android:text="白龙马"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent" />
        </RadioGroup>
    </LinearLayout>

    <TextView
        android:gravity="center"
        android:id="@+id/tv_result"
        android:layout_width="match_parent"
        android:layout_height="100dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70dp">
        <RadioGroup
            android:id="@+id/rg_lol"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <RadioButton
                android:background="@drawable/shape_frame"
                android:theme="@style/MyRadioButton"
                android:layout_margin="10dp"
                android:gravity="center"
                android:text="盖伦"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent" />
            <RadioButton
                android:background="@drawable/shape_frame"
                android:theme="@style/MyRadioButton"
                android:layout_margin="10dp"
                android:gravity="center"
                android:text="赵信"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent" />
            <RadioButton
                android:background="@drawable/shape_frame"
                android:theme="@style/MyRadioButton"
                android:layout_margin="10dp"
                android:gravity="center"
                android:text="嘉文"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent" />
            <RadioButton
                android:background="@drawable/shape_frame"
                android:theme="@style/MyRadioButton"
                android:layout_margin="10dp"
                android:gravity="center"
                android:text="薇恩"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent" />
        </RadioGroup>
    </LinearLayout>

    <TextView
        android:gravity="center"
        android:id="@+id/tv_result_lol"
        android:layout_width="match_parent"
        android:layout_height="100dp" />

</LinearLayout>

Look at the Java code again:


private RadioGroup mRgST;
    private RadioGroup mRgLOL;
    private TextView mTvResult;
    private TextView mTvResultLOL;
    /**
     * 定义变量记录选中adioButton的索引
     */
    private int position = -1;
    private int position_lol = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        initView();
    }

    private void initView() {
        mRgST = (RadioGroup) findViewById(R.id.rg_st);
        mRgLOL = (RadioGroup) findViewById(R.id.rg_lol);
        mTvResult = (TextView) findViewById(R.id.tv_result);
        mTvResultLOL = (TextView) findViewById(R.id.tv_result_lol);

        setRadioGroupCheckedListener(mRgST,1);
        setRadioGroupCheckedListener(mRgLOL,2);
//        mRgST.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
        //找到点击的RadioButton
        RadioButton radioButton = (RadioButton) findViewById(i);
        switch (radioGroup.getId()){
            case R.id.rg_st:
                //把RadioButton的文字展示到结果区
                mTvResult.setText(radioButton.getText());
                break;
        }
    }

    /**
     * 监听
     * @param radioGroup
     */
    private void setRadioGroupCheckedListener(final RadioGroup radioGroup, final int number) {
        //遍历RadioGroup中的所有子View也就是RadioButton
        for (int i = 0; i < radioGroup.getChildCount(); i++) {
            final RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
            final int finalI = i;
            //监听RadioButton的点击事件
            radioButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //以下逻辑是在RadioButton点击后(也就是RadioButton状态改变后)才执行
                    //清空RadioGroup的选中状态
                    radioGroup.clearCheck();
                    switch (number){
                        case 1:
                            //记录选中按钮文字的变量
                            String result = "";
                            //判断记录的值是否和点击的索引值是否相同
                            if (position == finalI){//相同:点击的是同一个RadioButton执行以下逻辑
                                radioButton.setChecked(false);//设置RadioButton选中状态为false
                                position = -1;//初始化position的值
                                result = "";//初始化result的值
                            }else {//不同:点击的是不同RadioButton
                                radioButton.setChecked(true);////设置RadioButton选中状态为true
                                position = finalI;//为position赋值
                                result = (String) radioButton.getText();//为result赋值
                            }
                            mTvResult.setText(result);//状态改变后把结果显示到结果区
                            break;
                        case 2:
                            //记录选中按钮文字的变量
                            String result_lol = "";
                            //判断记录的值是否和点击的索引值是否相同
                            if (position_lol == finalI){//相同:点击的是同一个RadioButton执行以下逻辑
                                radioButton.setChecked(false);//设置RadioButton选中状态为false
                                position_lol = -1;//初始化position的值
                                result_lol = "";//初始化result的值
                            }else {//不同:点击的是不同RadioButton
                                radioButton.setChecked(true);////设置RadioButton选中状态为true
                                position_lol = finalI;//为position赋值
                                result_lol = (String) radioButton.getText();//为result赋值
                            }
                            mTvResultLOL.setText(result_lol);//状态改变后把结果显示到结果区
                            break;
                    }
                }
            });
        }
    }
In fact, you can also add a button A to register the click event of the A button: call the ClearChecked () of the RadioGroup; you can also cancel the selected state, but in this case, the UI needs to be changed, and it may not be beautiful.



Published 6 original articles · received 1 · views 444

Guess you like

Origin blog.csdn.net/qq_35809004/article/details/79070755