0

I am having 2 RadioGroups in my layout.

I want user to check RadioButtons from both groups.

But I want user to select RadioButton from first group first and then from second group. So that if the user directly tries to select the RadioButton from second group it generates an error ( could be Toast or anything ) that First select the RadioButton from first group.

So I want to implement this thing so that user wont be able to select Radio Button from second group first.

e.g

Type:

o Easy o Difficult

Action:

o Add o Subtract

These are the radio buttons from different radio groups.

If the user directly tries to select radio button from the Action group then it wont gets selected and display and error message that First Select the type.

Hope I explained my problem clearly.

Thanks

4

1 回答 1

0

If you put this into your onCreate, adjusting for your IDs:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    RadioGroup groupOne = (RadioGroup) findViewById(R.id.radio_group_one);

    groupOne.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int index) {
            RadioGroup groupTwo = (RadioGroup) findViewById(R.id.radio_group_two);
            for(int i = 0; i < groupTwo.getChildCount(); i++) {
                groupTwo.getChildAt(i).setEnabled(true);
            }
        }
    });
}

And then your XML layout file should look something like this:

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

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Group 1 button 1"/>

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Group 1 button 2"/>
</RadioGroup>

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

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Group 2 button 1"
        android:enabled="false"/>

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Group 2 button 2"
        android:enabled="false"/>
</RadioGroup>

So the buttons in the second RadioGroup are disabled at first, and only becomes enabled after the user makes a selection in the first one.

于 2013-05-25T23:36:44.903 回答