Android RadioButton无法使用setChecked(false)方法取消设置 [英] Android RadioButton not able to unset using setChecked(false) method

查看:1281
本文介绍了Android RadioButton无法使用setChecked(false)方法取消设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

If I set a radio button to be selected on the first time, it works fine. But if I unselect it by calling ((RadioButton) findViewById(R.id.ID)).setChecked(false); then, later even if I try to make it selected by calling setChecked(true) will not work unless the user select it from the screen.

有人遇到吗?还是只有我一个?

Have any one come across this? or is it only me?

        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);
        }

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

If the else part is executed atleast once then everything gets mess up. When I step thro my debugger, I can see the execution goes in the correct path and setting it to true. It is getting executed only once, I checked that. And I am not resetting it back to false in any other part of the code.

推荐答案

我找到了解决方案.

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

It is not possible to uncheck a particular radio button. You can only set the other item to true.

因此,要清除所有选中的项目,应在RadioGroup上调用clearcheck()方法.

So to clear all the checked items, you should call the clearcheck() method on the RadioGroup.

所以我的其他部分是

        else {
            ((RadioGroup) findViewById(R.id.ID0)).clearCheck();
        }

这篇关于Android RadioButton无法使用setChecked(false)方法取消设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆