【问题标题】:Android radio button cant uncheckAndroid单选按钮无法取消选中
【发布时间】:2013-10-14 06:17:41
【问题描述】:

我需要一个可以选中和取消选中的单选按钮。

但是从我对堆栈溢出的读数来看,单选按钮将在选中后保持选中状态,因为需要选择 on 选项?

到目前为止,这是我的代码:

            RadioButton rButton = new RadioButton(getActivity());
            rButton.setText(listItems.get(i));
            rButton.setId(i);
            rButton.setChecked(false);
            rButton.setClickable(true);
            // rButton.setOnClickListener(new View.OnClickListener() {
            // @Override
            // public void onClick(View v) {
            // RadioButton rButton = (RadioButton)
            // layout.findViewById(position);
            // rButton.setChecked(!rButton.isChecked());
            // }
            // });              
            layout.addView(rButton);

即使使用注释掉的 onclick 侦听器,它也可以工作。

如何取消选中单选按钮?

【问题讨论】:

  • 单选按钮只需要检查集合中的一个即可。
  • 单选按钮在单选按钮组中(否则没有任何意义),单选按钮组使用clear 方法来删​​除选择。
  • (RadioButton) layout.findViewById(position);我不知道position是什么,但我怀疑它是radioButton的id

标签: android radio-button radio-group


【解决方案1】:

我认为您需要 CheckBox 代替。

【讨论】:

猜你喜欢
  • 2012-06-01
  • 2023-03-29
  • 2011-10-14
  • 2016-05-28
  • 1970-01-01
  • 1970-01-01
  • 2016-05-25
  • 1970-01-01
  • 2012-01-23
相关资源
最近更新 更多