Radio group is not working properly

I want to give the user four options: the first and second options on the line and the third and fourth options on the other line. My problem is when the application starts, I can choose several options, but I do not want this. This is my xml layout:

<RadioGroup
        android:id="@+id/rgAnswerQuestionChoices"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/rAnswerQuestonChoic1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="RadioButton"
                android:visibility="invisible" />

            <RadioButton
                android:id="@+id/rAnswerQuestionChoice2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="RadioButton"
                android:visibility="invisible" />
        </LinearLayout>

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/rAnswerQuestionChoice3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="RadioButton"
                android:visibility="invisible" />

            <RadioButton
                android:id="@+id/rAnswerQuestionChoice4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="RadioButton"
                android:visibility="invisible" />
        </LinearLayout>
    </RadioGroup>

What am I doing wrong?

+2
source share
2 answers

If you place other layouts between RadioButtonsand the parent Radiogroup(as you did with these LinearLayouts), then mutual exclusion will no longer work.

RadioButtons , Radiogroup, RadioButtons , , , RadioGroups, ( , RadioGroup , RadioButtons).

+3

, , Android. RadioGroup android, RadioButtons , RadioGroup . , RadioButtons LinearLayout , , ( , RadioButton ).

RadioGroupCheckListener.java :

RadioGroupCheckListener.makeGroup(radioButton1, radioButton2, radioButton3, radioButton4);

RadioGroupCheckListener.java github RadioGroupCheckListener

import android.widget.CompoundButton;


public class RadioGroupCheckListener implements CompoundButton.OnCheckedChangeListener {

    private CompoundButton[] allies;

    /**
     * public generator - indicate allies
     * @param allies all other buttons in the same RadioGroup
     */
    public RadioGroupCheckListener(CompoundButton... allies){
        this.allies = allies;
    }

    /**
     * listener for a button to turn other allies unchecked when it turn checked
     * @param buttonView change check occur with this button
     * @param isChecked result of changing
     */
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked){
            for (CompoundButton aly : allies) {
                aly.setChecked(false);
            }
        }
    }

    /**
     * inner private function to remove one element from Buttons array
     * @param buttons all the buttons in RadioGroup
     * @param me the index that we want to remove from array
     * @return an array of CompoundButtons except the index of me
     */
    private static CompoundButton[] exceptMe(CompoundButton[] buttons, int me){
        CompoundButton[] result = new CompoundButton[buttons.length-1];
        for(int i=0,j=0;i<buttons.length;i++){
            if(i==me){
                continue;
            }
            result[j]=buttons[i];
            j++;
        }
        return result;
    }

    /**
     * static function to create a RadioGroup
     * if a button turn to checked state all other buttons is group turn unchecked
     * @param buttons the buttons that we want to act like RadioGroup
     */
    public static void makeGroup(CompoundButton... buttons){
        for(int i=0;i<buttons.length;i++){
            buttons[i].setOnCheckedChangeListener(new RadioGroupCheckListener(exceptMe(buttons, i)));
        }
    }
}
0

Source: https://habr.com/ru/post/1622588/


All Articles