2017-11-05 4 views
0

RadioButtonsを次のコードを使用して既存の、しかし空のRadioGroupにプログラムで追加しています。次のようにプログラムで作成したRadioGroupの奇妙な振る舞い

 RadioGroup currencySettingRadioGroup = (RadioGroup) currency_settings_dialog.findViewById(R.id.rg_currency_symbol); 
     currencySettingRadioGroup.removeAllViews(); 

     RadioButton rb_none = new RadioButton(this); 

     // Add the 'None' option at the start 
     rb_none.setText("None"); 
     if (v_currency_symbol.equals("")) rb_none.setChecked(true); 
     currencySettingRadioGroup.addView(rb_none,0); 


     String[] currency_symbols_options_array = getResources().getStringArray(R.array.currency_symbols); 
     for (int i=0; i < currency_symbols_options_array.length; i++) { 
      RadioButton rb = new RadioButton(this); 
      rb.setText(currency_symbols_options_array[i]); 
      if (v_currency_symbol.equals(currency_symbols_options_array[i].substring(0,1))) rb.setChecked(true); 
      currencySettingRadioGroup.addView(rb,i+1); 
     } 

レイアウトXMLは次のとおりです。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/currency_settings_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:paddingBottom="12dp" 
    android:paddingLeft="24dp" 
    android:paddingRight="24dp" 
    android:paddingTop="24dp"> 

    <TextView 
     android:id="@+id/dialog_title" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/currency_symbol" 
     android:textAppearance="@android:style/TextAppearance.DeviceDefault.DialogWindowTitle" /> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/currency_symbol_explanation" /> 

    <RadioGroup 
     android:id="@+id/rg_currency_symbol" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"> 

    <Button 
     android:id="@+id/settings_close_button" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@android:color/transparent" 
     android:elevation="0dp" 
     android:gravity="end|center_vertical" 
     android:text="@string/close_currency_settings" 
     android:textColor="#008dcd" /> 
</LinearLayout> 

RadioGroupが正しく構築され、期待通りにチェックされている私のv_currency_symbol変数の最初の文字にマッチしたテキストでRadioButtonます。他のRadioButton秒のいずれかをクリックただし

Correct RadioButton selected when dialog opens

は、確認オプションをオフにすることはありません - 私がチェックした2つのオプションが終わります。

Clicking another option does not uncheck first

クリックすると他のオプションのいずれかをチェックするには、オフにする2番目のチェックの付いている位置を引き起こしますが、最初のRadioButtonが確認されたまま。

Other radio buttons behave like a separate radio group

これは、プログラム的にチェックされているRadioButtonが別々のラジオグループに属しているほとんどかのようです。

作成時にRadioButtonのいずれかをチェックする2つの行を削除すると、RadioGroupは正しく機能しますが、前の選択は表示されません。私は問題...がRadioGroupに追加する前にRadioButtonをチェックした

+0

あなたは問題がから来知るためにあなたのコードにデバッグがあります?あなたはそれを分離する理由ところで 'NONE'ラジオボタンをラジオグループの一つでなければなりません**'あなたはあなたは2つのチェックされたボタンを持つことを意味する同じラジオグループに2つのラジオボタンを追加 '** – Ibrahim

+0

すべては、固まったボタン以外はすべて正常に動作しているようです。 'RadioButtons'の' onClick'イベントが正しく起動されて 'v_currency_symbol'値が正しく設定され、ダイアログが閉じられて再び開かれたときに正しいme値がチェックされているように表示されますが、 'None'オプションは' RadioGroup'内にあり、forループによって読み込まれる配列からは作成されません。 –

答えて

0

は、問題が発生します。

関連する2行をスワップすると、問題が解決されます。次のように動作するコードは次のようになります?

// Add the 'None' option at the start 
    rb_none.setText("None"); 
    currencySettingRadioGroup.addView(rb_none,0); 
    if (v_currency_symbol.equals("")) rb_none.setChecked(true); 


    String[] currency_symbols_options_array = getResources().getStringArray(R.array.currency_symbols); 
    for (int i=0; i < currency_symbols_options_array.length; i++) { 
     RadioButton rb = new RadioButton(this); 
     rb.setText(currency_symbols_options_array[i]); 
     currencySettingRadioGroup.addView(rb,i+1); 
     if (v_currency_symbol.equals(currency_symbols_options_array[i].substring(0,1))) rb.setChecked(true); 
    }