ユーザーが必要な表示色を選択するためのダイアログボックスのビューに2つのラジオボタングループがあります。サイズの制約から2つのグループを作成しました。2つのグループは分離されたときに相互に排他的ではないため、group1からgroup2への変更を確認し、他のグループからの選択をクリアする必要があります。これを行うには、次のように各グループにonCheckedChangeListener()を追加します。
RadioGroup rGroup1 = (RadioGroup)currentDialog.findViewById(R.id.rgColors1);
rGroup1.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId != -1)
{
RadioGroup rGroup2 = (RadioGroup) currentDialog.findViewById(R.id.rgColors2);
rGroup2.clearCheck();
}
}
});
RadioGroup rGroup2 = (RadioGroup) currentDialog.findViewById(R.id.rgColors2);
rGroup2.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId != -1)
{
RadioGroup rGroup1 = (RadioGroup) currentDialog.findViewById(R.id.rgColors1);
rGroup1.clearCheck();
}
}
});
そして、私のダイアログレイアウトは次のようになります。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/colorDialogLinearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:minWidth="300dp"
android:orientation="vertical">
<TableLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tableLayout"
android:padding="5dp">
<TableRow android:gravity="center">
<RadioGroup android:id="@+id/rgColors1">
<RadioButton android:id="@+id/black_radio"
android:text="@string/option_black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton android:id="@+id/blue_radio"
android:text="@string/option_blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton android:id="@+id/green_radio"
android:text="@string/option_green"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton android:id="@+id/orange_radio"
android:text="@string/option_orange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton android:id="@+id/pink_radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/option_pink" />
</RadioGroup>
<RadioGroup android:id="@+id/rgColors2">
<RadioButton android:id="@+id/purple_radio"
android:text="@string/option_purple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton android:id="@+id/red_radio"
android:text="@string/option_red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton android:id="@+id/teal_radio"
android:text="@string/option_teal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton android:id="@+id/yellow_radio"
android:text="@string/option_yellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RadioGroup>
</TableRow>
</TableLayout>
<Button android:id="@+id/setBackColorButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/button_set_back_color"/>
いずれかのグループからラジオボタンを選択すると、そのグループに含まれていないグループがクリアされます。私が抱えている問題は、選択されたラジオボタンがチェック済みとして表示されないため、もう一度選択する必要があるということです。私の疑いは、clearCheck()が両方の無線グループに対して実行されていることですが、この動作を防ぐ理由や方法がわかりません。どんな助けでも大歓迎です。