How to uncheck or clear the radiobutton group?

对着背影说爱祢 提交于 2019-12-06 17:39:18

In my case I have a radiogroup named rb and 2 radiobuttons (rbY , rbN) inside of it.


So, this is my implementation using the onClickListener:

final RadioGroup rb = (RadioGroup) convertView.findViewById(R.id.creditcards);
                RadioButton rbY = (RadioButton) convertView.findViewById(R.id.radioButton);
                RadioButton rbN = (RadioButton) convertView.findViewById(R.id.radioButton2);

                final View finalConvertView = convertView;

                rbY.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int selected = rb.getCheckedRadioButtonId();
                        RadioButton rbYes = (RadioButton) finalConvertView.findViewById(selected);
                        RadioButton rbN = (RadioButton) finalConvertView.findViewById(R.id.radioButton2);

                        if (rbYes.isSelected()) {
                            rbYes.setSelected(false);
                            rb.clearCheck();
                            rb.clearChildFocus(rbYes);
                        } else {
                            rbYes.setSelected(true);
                        }
                    }
                });

                rbN.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int selected = rb.getCheckedRadioButtonId();
                        RadioButton rbNo = (RadioButton) finalConvertView.findViewById(selected);

                        if (rbNo.isSelected()) {
                            rbNo.setSelected(false);
                            rb.clearCheck();
                            rb.clearChildFocus(rbNo);
                        }
                        else {
                            rbNo.setSelected(true);
                        }
                    }
                });

I found this to be useful https://github.com/ragunathjawahar/deselectable-radio-button. It's a custom radio button that will allow for you to de-select an option thats already been checked.

Michał Z.

To check/uncheck radio button you can use radioButton.setChecked(true); (or false to uncheck)

Change your code like find id's for all radio buttons in onCreate() and make it as global...

in the onClickListener of next button write radioButton.setChecked(false); for all your radio buttons what you used...and remove the line btn_practicerg.clearCheck();

i tried like this working fine..

Ramesh R

Create clearRadioButtons method and call in onCreate.

public void clearRadioButtons() {
    radiogroup.clearCheck();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!