2017-11-05 15 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

는 확인 옵션을 선택 해제 발생하지 않습니다 - 나는 선택이 옵션을 끝낸다.

Clicking another option does not uncheck first

클릭 다른 옵션을 체크는 취소하는 두 번째 위치 확인을 야기하지만, 상기 제 RadioButton 체크 유지.

Other radio buttons behave like a separate radio group

는 거의 프로그래밍 방식으로 확인 별도을 radioGroup에 속해있는 RadioButton 것처럼입니다.

RadioButton 중 하나를 선택하여 생성시 두 개의 라인을 제거하면 RadioGroup이 올바르게 작동하지만 분명히 이전 선택을 볼 수 없습니다.

+0

당신이 문제에서 온 알고 당신의 코드를 디버깅 있나요? 당신이 그것을 분리하는 이유 BTW은'None' 라디오 버튼을 radioGroup 중 하나 여야합니다 **'당신은 동일한 radioGroup에 두 개의 라디오 버튼을 추가 했으므로 체크 된 두 개의 버튼이 표시됩니다. ** – Ibrahim

+0

멈춘 버튼 이외의 모든 것이 올바르게 작동하는 것 같습니다. 'RadioButtons'의'onClick' 이벤트가 올바르게 발령되어'v_currency_symbol' 값을 올바르게 설정하고 대화 상자가 닫히고 다시 열리면 올바른 me 값이 체크 된 것으로 표시되지만이 값은 그대로 유지됩니다. 그리고'None' 옵션은'RadioGroup' 안에 있고 for 루프에 의해 읽혀지는 배열로부터 만들어지지 않습니다. –

답변

0

문제점을 발견했습니다 ... RadioButton을 확인하기 전에RadioGroup에 추가하면 문제가 발생합니다.

두 개의 관련 줄을 바꿔서 문제를 해결합니다. 다음과 같이 작업 코드는 다음과 같습니다?

// 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); 
    }