Only one radio button selected at a time

≡放荡痞女 提交于 2019-12-09 18:31:34

问题


I have two radio button in a radio group. I also have 2 androd:button checkbox- for when the radio button is deselected and checkbox_v for when he user selects the checkbox. I also implemnted that a method onRadioButtonClick in order to make sure that only one radio button had drawable:checkbox and the other has checkbox_v . How can I implemnt onRadioClick to do this? any idea?

public void onRadioButtonClick(View v)
{
    RadioButton button = (RadioButton) v;
    boolean checkBox1Selected;
    boolean checkBox2Selected = false;

    Toast.makeText(MainActivity.this,
        button.getId()+ " was chosen."+R.id.wificheckBox,
        Toast.LENGTH_SHORT).show();

    if ( button.getId() ==R.id.wificheckBox) {
        // Toggle status of checkbox selection
        checkBox1Selected = radiowifiButton.isChecked();

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

main xml

<RadioGroup
    android:id="@+id/radioGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true"
    android:layout_below="@+id/ll_1"
    android:layout_marginLeft="20dp">

<LinearLayout
    android:id="@+id/ll_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/ll_1"
    android:layout_marginLeft="20dp"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/wifimobilecheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/button_radio"
        android:checked="true"
        android:onClick="onRadioButtonClick" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/wificheckBox"
        android:layout_toRightOf="@+id/wificheckBox"
        android:paddingLeft="15dp"
        android:text="WiFi or mobile network"
        android:textColor="#333333"
        android:textSize="20dp" />
</LinearLayout>

<LinearLayout
    android:id="@+id/ll_3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/ll_2"
    android:paddingTop="20dp"
    android:layout_marginLeft="20dp"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/wificheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/button_radio"

        android:onClick="onRadioButtonClick" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/wificheckBox"
        android:layout_toRightOf="@+id/wificheckBox"
        android:paddingLeft="15dp"
        android:text="WiFi "
        android:textColor="#333333"
        android:textSize="20dp" />
</LinearLayout>

</RadioGroup>

Drawabe- button_radio

<item android:state_checked="true"android:state_pressed="false" android:drawable="@drawable/checkbox_v"/><item android:state_checked="false"android:state_pressed="false"'android:drawable="@drawable/checkbox"/>

回答1:


If they are in a radiogroup, only one can be selected at a time. If you want both to be able to be selected, remove them from the radiogroup.




回答2:


As the top voted answer didn't work for me as for many others. I am sharing the method that i used and its working perfectly.

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

            <RadioButton
                android:id="@+id/rb_female"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="10dp"
                android:layout_marginRight="10dp"
                android:button="@drawable/selector_radio_button"
                android:text="Female" />

            <RadioButton
                android:id="@+id/rb_male"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:button="@drawable/selector_radio_button"
                android:text="Male" />
</RadioGroup>

Setting the android:checkedButton="@+id/rb_female" worked for me in RadioGroup.




回答3:


You should remove your onRadioButtonClick method and add OnCheckedChangeListener to your radiogroup. http://developer.android.com/reference/android/widget/RadioGroup.OnCheckedChangeListener.html

I guess by implementing your own click handler your are overwriting some functionality of the radiogroup.




回答4:


Your requirement is not clear. Would you mind to make it simpler. I tried my best to understand your requirements and these are the findings given below :(.

A radio button can have text. So you don't have to add another TextView in your layout. Please remove them and add a simple android:text="blah.. blah.." to your radiobutton.

With my understanding you have two doubts.

How to customize RadioButtons with custom drawables??

Checkout the answer here.

How to deselect the previously selected radiobutton??

The truth is you don't have to do it manually as you are keeping those radiobuttons into a single radiogroup.

Hope this will help you.




回答5:


To check/uncheck radio buttons not part of a RadioGroup, add a method in the android GUI designer under the RadioButton Properties 'onClick' (In this example radio1_onClick).

In your activity.xml under the xml for the RadioButton

RadioButton 
    [blah blah blah]
    android:id="@+id/radio1"
    android:onClick="radio1_onClick"

In your activity_main.xml you would write the following method.

private void radio1_onClick(View v)
{
    CompoundButton b=(CompoundButton)findViewById(R.id.radio1);
    b.setChecked(true); /* light the button */

    //b.setChecked(false); /* unlight the button */
}


来源:https://stackoverflow.com/questions/10107532/only-one-radio-button-selected-at-a-time

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