以编程方式创建的RadioGroup的奇怪行为 [英] Strange behaviour of programatically created RadioGroup
问题描述
我正在通过编程将 RadioButtons
添加到预先存在但为空的 RadioGroup
中,并使用以下代码。
I am programatically adding RadioButtons
to a pre-existing but empty RadioGroup
with the following code.
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
变量的第一个字符匹配的文本,将按预期进行检查。
The RadioGroup
gets built correctly and the RadioButton
with the text that matches my v_currency_symbol
variable's first character is checked as expected.
但是,单击其他任何 RadioButton
s不会导致选中的选项取消选中-我最终选中了两个选项。
However, clicking any of the other RadioButton
s does not cause the checked option to uncheck - I end up with two options checked.
单击并选中其他任何选项都会取消选中第二个选中的位置,但是第一个 RadioButton
保持选中状态。
Clicking and checking any of the other options causes the second checked position to uncheck, but the first RadioButton
remains checked.
以编程方式检查的 RadioButton
几乎就像是一个单独的RadioGroup。
It is almost as though the RadioButton
that is checked programatically belongs to a seperate RadioGroup.
删除创建时检查 RadioButton
之一的两行,将允许 RadioGroup
正常运行,但是您显然看不到先前的选择。
Removing the two lines that check one of the RadioButton
s on creation allows the RadioGroup
to function properly, but you obviously cannot then see the previous selection.
推荐答案
我发现了问题...正在检查 RadioButton
之前将其添加到 RadioGroup
会导致问题。
I found the issue... checking the RadioButton
before adding it to the RadioGroup
causes the problem.
交换两个相关行即可解决此问题。工作代码如下:
Swapping the two relevant lines resolves the issue. The working code is as follows:
// 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);
}
这篇关于以编程方式创建的RadioGroup的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!