0

ユーザーが必要な表示色を選択するためのダイアログボックスのビューに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()が両方の無線グループに対して実行されていることですが、この動作を防ぐ理由や方法がわかりません。どんな助けでも大歓迎です。

4

1 に答える 1

1

最終的に、ラジオボタンごとにonclick()イベントを作成する必要がありました。私の最初のアプローチが機能しないと思う理由は、選択をnoneに設定するとonselectchange()イベントが発生するためです。2番目のグループをnoneに設定するこの方法を使用するさまざまな方法をテストする際に、2回目の選択を実行するか、2つのグループの無限ループを互いに設定してnoneにする必要がありました。もう一度選択を行う必要があるのは、プロシージャが呼び出し元のプロシージャに制御を戻して選択を終了しないためだと思います。私はAndroidに少し慣れているので、完全にベースから外れる可能性があります。ただし、最終的には、個別のonclick()イベントが機能します。

于 2012-09-06T21:37:19.837 に答える