【问题标题】:How to uncheck RadioButton if already checked in RadioGroup如果已在 RadioGroup 中选中,如何取消选中 RadioButton
【发布时间】:2020-06-16 10:05:34
【问题描述】:

我在 RadioGroup 中有两个 RadioButton,最初都未选中。如果已选中 RadioButton,我想取消选中它。我已经尝试过,但我无法做到。当我选择任何单选按钮时,它不会将状态更改为第一次选中,如果我第二次选择相同的单选按钮,那么它会改变其状态,如果我选择其他单选按钮,如果一个按钮被选中,那么它也不会改变状态第一次和所有按钮都未选中有人可以帮助我吗?我的代码如下...

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
RadioButton checkedRadioButton = (RadioButton)group.findViewById(checkedId);
pus="";

boolean isChecked = checkedRadioButton.isChecked();
 if(isChecked){
     checkedRadioButton.setChecked(false);

 }else{
     checkedRadioButton.setChecked(true);

 }
}
 Toast.makeText(context, pus, Toast.LENGTH_SHORT).show();
}
});
<RadioGroup
    android:id="@+id/rdbt_group"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp"

    android:gravity="center"
    android:orientation="horizontal" >

    <RadioButton
    android:id="@+id/radio_no"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@drawable/rbtn_selector"
    android:button="@null"
    android:gravity="center"
    android:paddingBottom="5dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="5dp"
    android:clickable="true"
    android:textColor="@drawable/rbtn_textcolor_selector"
    android:textSize="15sp" />

    <RadioButton
    android:id="@+id/radio_yes"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:button="@null"
    android:clickable="true"
    android:textSize="15sp"
    android:background="@drawable/rbtn_selector"
    android:textColor="@drawable/rbtn_textcolor_selector"
    android:padding="5dp"
    android:gravity="center"
    />
</RadioGroup>

【问题讨论】:

  • 如果我没记错你想要的如果用户再次点击相同的选中 RadioButton,它应该清除选择,对吧?
  • 是的,你是对的......

标签: android radio-button radio-group


【解决方案1】:

我知道现在回答这个问题有点晚了,但如果它可以帮助其他人,我会很高兴。

只需创建一个自定义类扩展 RadioButtonAppCompatRadioButton 并覆盖 toggle() 方法。

public class UncheckableRadioButton extends AppCompatRadioButton {

    public UncheckableRadioButton(Context context) {
        super(context);
    }

    public UncheckableRadioButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public UncheckableRadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void toggle() {

        if (isChecked()) {
            if (getParent() != null && getParent() instanceof RadioGroup) {
                ((RadioGroup) getParent()).clearCheck();
            }
        } else {
            super.toggle();
        }
    }

    @Override
    public CharSequence getAccessibilityClassName() {
        return UncheckableRadioButton.class.getName();
    }
}

如果您检查radioGroup.getCheckedRadioButtonId(),这也将起作用。

【讨论】:

    【解决方案2】:

    我只有一个单选按钮,我真的不想制作单选按钮组。 onClick 和 if 和 else 检查无法手动检查和取消选中它。我读了几篇关于 onRadioButtonClick 监听器等的帖子。

    首先我尝试切换到复选框-> 同样的问题。然后我尝试了 CheckedTextView,你知道吗,它实际上就像一个切换开关。所以基本上我添加了一个单选按钮然后将文本留空,在它旁边添加了checkedTextView(重叠)然后将checkedTextView的onClick函数链接到一个切换函数->

    public void toggle(View view)
    { 
        if(checkedTextView.isChecked())
        { 
            checkedTextView.setChecked(false); 
            actualRadioBtn.setChecked(false);
        }
        else
        {
            checkedTextView.setChecked(true);
            actualRadioBtn.setChecked(true);
        }
    }
    

    然后在您检查该单选按钮是否被选中的函数中,您可以简单地使用

    if(actualRadioBtn.isChecked())....
    

    【讨论】:

    • 你看过 Kotlin 吗?
    【解决方案3】:

    RadioGroup 用于选择两个选项之一,您不应干扰视图的常规行为。用户强制从两个选项中选择一个。将性别视为 radiogroup ,可能有两个或三个选项,您必须强制选择其中一个。这可以通过简单地在 xml 中添加以下代码来实现

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        app:layout_constraintLeft_toLeftOf="parent">
    
        <RadioButton
          android:id="@+id/radioButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="RadioButton"
          tools:layout_editor_absoluteX="126dp"
          tools:layout_editor_absoluteY="83dp"/>
    
        <RadioButton
          android:id="@+id/radioButton2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="RadioButton"
          tools:layout_editor_absoluteX="172dp"
          tools:layout_editor_absoluteY="127dp"/>
      </RadioGroup>
    

    在活动课上

    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
        {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // checkedId is the RadioButton selected
                // you can check if radiobutton is checked or unchecked here , do needful codes
            }
        });
    

    【讨论】:

      【解决方案4】:

      您不能“取消选中”单选按钮。但是您可以使用clearCheck 清除RadioGroup

      【讨论】:

        【解决方案5】:

        我建议您改用 CheckBox。希望对你有帮助

        【讨论】:

        • 您能否提供更多详细信息,为什么 CheckBox 是更好的选择? TIA。
        • 是的。您实际上可以默认取消选中复选框。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-14
        • 1970-01-01
        • 2014-02-03
        • 2021-09-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多