如何在RadioGroup中取消选中已选中的RadioButton

3

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

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,它应该清除选择,对吗? - Chintan Soni
是的,你是正确的... - Tushar Kotecha
2
可能是如果已经选中,则取消单选按钮?的重复问题。 - Narendra Singh
6个回答

13

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

只需创建一个扩展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函数链接到一个toggle函数中 ->

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了吗? - IgorGanapolsky

1

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

0

这是一种取消选中单选按钮的方法。

创建一个实例变量来存储上次选中的单选按钮ID:

int lastChecked = radioGroup.getCheckedRadioButtonId();

然后:

View.OnClickListener radioClick = radioButton -> {
    if( radioButton.getId() == lastChecked ) {
        radioGroup.clearCheck();
    }
};
findViewById(R.id.radio_no).setOnClickListener(radioClick);
findViewById(R.id.radio_yes).setOnClickListener(radioClick);

radioGroup.setOnCheckedChangeListener((group, checked) -> {
    group.post(() -> {
        lastChecked = checked;
    });
});

0

0
我建议您使用复选框。希望能帮到您。

你能提供更多关于为什么CheckBox是更好的选择的细节吗?谢谢。 - benc
当然可以。实际上你可以默认取消复选框。 - Paul Addai

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接