可以在Android应用中取消选中所有单选按钮吗?

waj*_*jiw 5 android radio-group radio-button

只是想知道是否有人知道是否有一个radiogroup或radiobutton属性或其他快速的东西,允许单选按钮在处于检查模式时取消选中.我正在寻找像无线电组一样工作的功能(即只能检查一个),但我也希望它们能够全部取消选中.

Bor*_*jev 11

也许我不会在这里得到问题,但这是我想做的事情.我有一个活动,我用它来分类很多图片.我正在使用单选按钮进行分类.在用户检查其中一个选项后,他可以用下一张图片切换.我需要在切换图片时清除选择,但决定不创建新活动.

所以我在布局中初始化我的无线电组是这样的:

<RadioGroup
    android:id="@+id/radio_selection"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/radio_true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:text="@string/true" />

    <RadioButton
        android:id="@+id/radio_false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:text="@string/false" />
</RadioGroup>
Run Code Online (Sandbox Code Playgroud)

这会初始化我的无线电组,并且RadioButton最初都没有选中.之后当我更改图片时,我需要清除选择(因为用​​户尚未选择新图片).我喜欢这样:

RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radio_selection);
radioGroup.clearCheck();
Run Code Online (Sandbox Code Playgroud)

这正是我所需要的:再次制作没有选中的单选按钮.我希望我理解这个问题,这将有助于将来的某些人.

  • @wajiw是的我知道这是一个非常古老的问题.我只是添加自己的位.SO的目的不是问问题,当你想出一个解决方案时,请自己保留它 - 只需在这里分享您找到的内容:http://xkcd.com/979/ (6认同)

Man*_*del 3

您可以使用复选框来模仿您想要的功能,如下所示。该代码假定您有两个复选框,但也可以有两个以上。

public void onClick(View v) {
    int id = v.getId();
    if (id == R.id.checkBox1) {
        // Toggle status of checkbox selection
        checkBox1Selected = checkBox1.isChecked();

        // Ensure that other checkboxes are not selected
        if (checkBox2Selected) {
            checkBox2.setChecked(false);
            checkBox2Selected = false;
         } 
    else if (id == R.id.checkBox2) {
         // Toggle status of checkbox selection
         checkBox2Selected = checkBox2.isChecked();

        // Ensure that other checkboxes are not selected
        if (checkBox1Selected) {
            checkBox1.setChecked(false);
            checkBox1Selected = false;
        }
}
Run Code Online (Sandbox Code Playgroud)