代码之家  ›  专栏  ›  技术社区  ›  franklins

Android单选按钮无法使用setChecked(false)方法取消设置

  •  22
  • franklins  · 技术社区  · 14 年前

    如果我将单选按钮设置为第一次选中,它工作正常。但如果我通过打电话取消选择 ((单选按钮)findViewById(R.id.id)).setChecked(false);

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

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

    当我在调试器中单步执行时,我可以看到执行的路径是正确的,并将其设置为true。只有一次被处决,我查过了。我不会在代码的其他部分将其重置为false。

    2 回复  |  直到 11 年前
        1
  •  52
  •   franklins    14 年前

    我找到了解决办法。

    无法取消选中特定单选按钮。只能将另一项设置为true。

    所以要清除所有选中的项目,您应该调用 clearcheck()

    所以我的另一部分是

            else {
                ((RadioGroup) findViewById(R.id.ID0)).clearCheck();
            }
    
        2
  •  2
  •   Kushan    12 年前

    拿一个看不见的单选按钮检查一下。组的所有其他单选按钮将自动取消选中。。