Unchecking a radio button

我是研究僧i 提交于 2019-12-04 10:15:00
spaaarky21

Please see my answer to your other basically identical question, which I've copied below... :)

I recently had the same need - to have a radio group where the selected item could be deselected by tapping it again. I found that I couldn't accomplish that using listeners but I was able to do it using a custom RadioButton, like so...

public class ToggleableRadioButton extends RadioButton {
    // Implement necessary constructors

    @Override
    public void toggle() {
        if(isChecked()) {
            if(getParent() instanceof RadioGroup) {
                ((RadioGroup)getParent()).clearCheck();
            }
        } else {
            setChecked(true);
        }
    }
}

Notice that the button is toggled in different ways depending on its current state - i.e., calling setChecked(true) on the button vs. calling clearCheck() on the group. If setChecked() is used in both cases, a button that was just deselected cannot be immediately re-selected - the logic in RadioGroup prevents it.

To use this button, just replace your <RadioButton> tags with <your.package.ToggleableRadioButton> in your layout XML.

You need to put the button in a radio group, then clear the group. A radio button can't be unchecked directly, because the idea is that one option in a group is always checked.

Here is an example of how to create radiogroup, radiobutton and how to use in Java code. Hope it will give you complete picture

XML file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <RadioGroup
        android:id="@+id/maptype"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/line1"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/map"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:drawableRight="@drawable/ic_launcher"/>

        <RadioButton
            android:id="@+id/satellite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableRight="@drawable/ic_launcher"/>
    </RadioGroup>

</LinearLayout>

In Java Code

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    RadioGroup rgrpMapType = (RadioGroup) findViewById(R.id.maptype);
    int selectedTypeId = rgrpMapType.getCheckedRadioButtonId();
    RadioButton rbMapType = (RadioButton) findViewById(selectedTypeId);
    if(rbMapType != null) // This will be null if none of the radio buttons are selected
           rgrpMapType.clearCheck(); 
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!