0

次のコードを使用して、実行時に(列挙から)ラジオボタンを使用してラジオグループを作成しています。

RadioGroup radioGroup = new RadioGroup(this);

List<LocationTypeEnum> warningTypes = preferences.getWarningTypes();
for (LocationTypeEnum enumElement : warningTypes) {
    RadioButton radio = new RadioButton(this);
    radio.setText(enumElement.toString());

    //Check one specific radio by default
        radio.setChecked(enumElement.intValue == userDefinedLocation.getType().intValue);

    radioGroup.addView(radio, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
}

画面に関しては、ラジオを変更しようとすると、両方のオプションがチェックされたままになります。

ここに画像の説明を入力してください

何が問題なのですか?

4

2 に答える 2

3

Found the answer. You must add the RadioButton to the RadioGroup before setting it to checked, or else the radio group gets lost. Following, the correct code.

    RadioGroup radioGroup = new RadioGroup(this);

    List<LocationTypeEnum> warningTypes = preferences.getWarningTypes();
    for (LocationTypeEnum enumElement : warningTypes) {
        RadioButton radio = new RadioButton(this);
        radio.setText(enumElement.toString());

        //First, add the radio to the group
        radioGroup.addView(radio, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        //Only after that you can check it.
        radio.setChecked(enumElement.intValue == userDefinedLocation.getType().intValue);

    } 

Sounds like a bug, to me. Credits to http://code.google.com/p/android/issues/detail?id=1772

于 2013-01-15T22:05:46.573 に答える
0

Don't know how to comment to ask you to post your layout .xml file. Make sure that you also group them in your xml file like below:

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

        <RadioButton
            android:id="@+id/radioMale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_male" 
            android:checked="true" />

        <RadioButton
            android:id="@+id/radioFemale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_female" />

    </RadioGroup>
于 2013-01-15T20:11:54.897 に答える