开发者

Possible to allow all radio buttons to be unchecked in an Android app?

Just wanted to see if anyone knows if there's a radiogroup or radiobutton attribute or something else quick that will allow radio buttons to be unchecked when they're in checked mode. I'm looking to build functionality that works like a radio group (i.e. only one can be checked) but I also want开发者_StackOverflow them to be able to be all unchecked.


Maybe I don't get the question here, but this is something I wanted to do. I have a single activity that I use to classify many pictures. I am classifying using radio buttons. After the user checks one of the options he is allowed to switch with the next picture. I needed to clear the selection, when switching the picture, but decided not to create new activity.

So I initialize my radio group in the layout like that:

<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>

This initializes my radio group and both RadioButton are not selected initially. Afterwards when I change the picture I need to clear the selection (because user has not yet chosen for the new picture). I do like that:

RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radio_selection);
radioGroup.clearCheck();

This is doing exactly what I need: making again none of the radio buttons being selected. I hope I understood the question and this will help somebody in the future.


You can use a CheckBox to mimic the functionality you want as shown below. The code assumes that you have two check boxes but you could have more than two.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜