以编程方式创建的 RadioGroup 的奇怪行为
Strange behaviour of programatically created RadioGroup
我正在使用以下代码以编程方式将 RadioButtons
添加到预先存在但为空的 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
构建正确,RadioButton
与我的 v_currency_symbol
变量的第一个字符匹配的文本按预期检查。
但是,单击任何其他 RadioButton
不会导致取消选中已选中的选项 - 我最终选中了两个选项。
单击并选中任何其他选项会导致取消选中第二个选中位置,但第一个 RadioButton
保持选中状态。
几乎就像以编程方式检查的 RadioButton
属于一个单独的 RadioGroup。
在创建时删除检查 RadioButton
之一的两行允许 RadioGroup
正常运行,但您显然无法看到之前的选择。
我发现了问题...在 将其添加到 RadioGroup
之前检查 RadioButton
会导致问题。
交换两条相关线即可解决问题。工作代码如下:
// 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);
}
我正在使用以下代码以编程方式将 RadioButtons
添加到预先存在但为空的 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
构建正确,RadioButton
与我的 v_currency_symbol
变量的第一个字符匹配的文本按预期检查。
但是,单击任何其他 RadioButton
不会导致取消选中已选中的选项 - 我最终选中了两个选项。
单击并选中任何其他选项会导致取消选中第二个选中位置,但第一个 RadioButton
保持选中状态。
几乎就像以编程方式检查的 RadioButton
属于一个单独的 RadioGroup。
在创建时删除检查 RadioButton
之一的两行允许 RadioGroup
正常运行,但您显然无法看到之前的选择。
我发现了问题...在 将其添加到 RadioGroup
之前检查 RadioButton
会导致问题。
交换两条相关线即可解决问题。工作代码如下:
// 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);
}