Unchecking a radio button

不羁的心 提交于 2019-12-06 05:21:02

问题


The application is a step sequencer application with 16 radio groups with 8 buttons in each group. It works perfectly except once a group has a button selected I cant turn it off unless I use the clear button I have created to clear all radiogroups. What I would like to add is some code that says when a selected radio button is selected again it simply turns off like a toggle. I tried using toggles but then other issues arose with that method. Below is an attempt at it but it is way off the mark Im guessing

final RadioGroup radioGroup1 = (RadioGroup)findViewById(R.id.RadioGroup1);
RadioButton lC1 = (RadioButton)findViewById(R.id.RadioButtonlowC1);

Button D1 = (Button)findViewById(R.id.RadioButtonD1);
        D1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                PdBase.sendFloat("D1", 74);
                int selectedTypeId = radioGroup1.getCheckedRadioButtonId();
                RadioButton D1 = (RadioButton) findViewById(selectedTypeId);
                if(radioGroup1 != null) // This will be null if none of the radio buttons are selected
                       radioGroup1.clearCheck(); 
                PdBase.sendFloat("D1", 0);
            }
        });

回答1:


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.




回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/15821334/unchecking-a-radio-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!