Android RadioButton无法使用setChecked(false)方法取消设置

fra*_*ins 22 android radio-button uncheck

如果我第一次设置一个单选按钮,它可以正常工作.但如果我通过调用((RadioButton)findViewById(R.id.ID))取消选择它.setChecked(false); 然后,即使我尝试通过调用setChecked(true)来选择它也不会工作,除非用户从屏幕上选择它.

有没有人碰到过这个?还是只有我?

        if(Val != null){
        if( ((RadioButton) findViewById(R.id.ID1)).getText().toString().trim().equals(Val))
        ((RadioButton) findViewById(R.id.ID1)).setChecked(true);
        else if(((RadioButton) findViewById(R.id.ID2)).getText().toString().trim().equals(Val))
        ((RadioButton) findViewById(R.id.ID2)).setChecked(true);
        }
        else {
            ((RadioButton) findViewById(R.id.ID1)).setChecked(false);
            ((RadioButton) findViewById(R.id.ID2)).setChecked(false);
        }
Run Code Online (Sandbox Code Playgroud)

如果else部分至少执行一次,那么一切都会变得混乱.当我进入调试器时,我可以看到执行进入正确的路径并将其设置为true.它只被执行一次,我检查了一下.而且我没有在代码的任何其他部分将其重置为false.

fra*_*ins 51

我找到了解决方案.

无法取消选中特定的单选按钮.您只能将其他项目设置为true.

因此,要清除所有已检查的项目,您应该clearcheck()在RadioGroup上调用该方法.

所以我的其他部分是

        else {
            ((RadioGroup) findViewById(R.id.ID0)).clearCheck();
        }
Run Code Online (Sandbox Code Playgroud)