【问题标题】:Uncheck all RadioButton in a RadioButtonGroup取消选中 RadioButtonGroup 中的所有 RadioButton
【发布时间】:2019-02-05 17:29:24
【问题描述】:

我想要实现的是:在活动开始后,我希望没有选择/选中任何 RadioButton。

我的问题是:当活动开始时,第一个 RadioButton 总是被选中/选中。

我在单选按钮初始化后(在 onCreate 内)尝试了radioButton1.setChecked(false),但是当活动开始时,我无法手动检查/选择第一个单选按钮。直到我选择了第二个或第三个单选按钮,我现在可以选择/检查第一个单选按钮。

【问题讨论】:

    标签: android android-radiobutton


    【解决方案1】:
    RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radiogroup);
    radioGroup.clearCheck();
    

    【讨论】:

    • 据我所知,没有名为“RadioButtonGroup”的类。取而代之的是,适当的类名是“RadioGroup”。除了这个微小的事实,上面的代码是完全可行的!
    • 如果我们想在 OnCheckedChangeListener 中这样做呢?对于未来的问题,您为什么要这样做,除了广播 btn 之外,我在广播 grp 中有不同的类型视图!
    • 有没有办法在没有古怪的取消选中动画的情况下做到这一点?
    【解决方案2】:

    使用 clearCheck() 在活动开始或恢复时清除所有选中的单选按钮

     @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     RadioGroup rg=(RadioGroup)findViewById(R.id.RG);
    rg.clearCheck();
    }
    @Override
    protected void onResume() {  
    RadioGroup rg=(RadioGroup)findViewById(R.id.RG);
    rg.clearCheck();  
    super.onResume();  
        }  
    

    【讨论】:

    • 设置此单选按钮背景可绘制选择器后不起作用
    【解决方案3】:

    使用这个

    RadioButton spec1=findViewById(yourRadioGroup.getCheckedRadioButtonId());
            if (spec1.isChecked())
            {
                spec1.setChecked(false);
            }
    

    【讨论】:

      猜你喜欢
      • 2011-07-09
      • 1970-01-01
      • 2011-02-23
      • 2016-01-21
      • 2011-05-22
      • 2019-12-07
      • 1970-01-01
      • 2021-09-25
      • 2012-07-15
      相关资源
      最近更新 更多