使用字符串数组中的 RadioButtons 填充 RadioGroup
Populate a RadioGroup with RadioButtons from a string-array
我有一个包含空 RadioGroup
的布局。 (我认为这是这个问题和其他被问到的问题之间的区别——我已经在我的布局中的正确位置放置了空的 RadioGroup)
我想用我在 strings.xml
.
中定义的 string-array
中的 item
填充此 RadioGroup
strings.xml
中的数组如下所示:
<string-array name="currency_symbols">
<item>$ - Dollar</item>
<item>€ - Euro</item>
<item>£ - Pound</item>
<item>¥ - Yen</item>
<item># - Other</item>
</string-array>
然后我尝试创建一个 RadioButton
并将其添加到 RagioGroup 中,如下所示:
RadioGroup currencySettingRadioGroup = (RadioGroup) settings_dialog.findViewById(R.id.rg_currency_symbol);
currencySettingRadioGroup.removeAllViews();
RadioButton rb = new RadioButton(this);
String[] currency_symbols_options_array = getResources().getStringArray(R.array.currency_symbols);
for ( String this_currency_option: currency_symbols_options_array ) {
rb.setText(this_currency_option);
currencySettingRadioGroup.addView(rb);
}
添加 currencySettingRadioGroup.removeAllViews();
是因为我收到以下错误,但这没有区别:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
显然导致问题的行是 currencySettingRadioGroup.addView(rb);
行...
如何正确执行此操作?
(我查看了 and the referenced http://android.okhelp.cz/create-radiobutton-radiogroup-dynamically-android-sample/ 但似乎无法正常工作)
根据 Barns52 的评论,每次围绕 for
-循环创建一个新的 RadioButton
(而不是在 for
-循环开始之前只创建一次)解决了这个问题。
工作代码如下:
RadioGroup currencySettingRadioGroup = (RadioGroup) settings_dialog.findViewById(R.id.rg_currency_symbol);
String[] currency_symbols_options_array = getResources().getStringArray(R.array.currency_symbols);
for ( String this_currency_option: currency_symbols_options_array ) {
RadioButton rb = new RadioButton(this);
rb.setText(this_currency_option);
currencySettingRadioGroup.addView(rb);
}
我有一个包含空 RadioGroup
的布局。 (我认为这是这个问题和其他被问到的问题之间的区别——我已经在我的布局中的正确位置放置了空的 RadioGroup)
我想用我在 strings.xml
.
string-array
中的 item
填充此 RadioGroup
strings.xml
中的数组如下所示:
<string-array name="currency_symbols">
<item>$ - Dollar</item>
<item>€ - Euro</item>
<item>£ - Pound</item>
<item>¥ - Yen</item>
<item># - Other</item>
</string-array>
然后我尝试创建一个 RadioButton
并将其添加到 RagioGroup 中,如下所示:
RadioGroup currencySettingRadioGroup = (RadioGroup) settings_dialog.findViewById(R.id.rg_currency_symbol);
currencySettingRadioGroup.removeAllViews();
RadioButton rb = new RadioButton(this);
String[] currency_symbols_options_array = getResources().getStringArray(R.array.currency_symbols);
for ( String this_currency_option: currency_symbols_options_array ) {
rb.setText(this_currency_option);
currencySettingRadioGroup.addView(rb);
}
添加 currencySettingRadioGroup.removeAllViews();
是因为我收到以下错误,但这没有区别:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
显然导致问题的行是 currencySettingRadioGroup.addView(rb);
行...
如何正确执行此操作?
(我查看了
根据 Barns52 的评论,每次围绕 for
-循环创建一个新的 RadioButton
(而不是在 for
-循环开始之前只创建一次)解决了这个问题。
工作代码如下:
RadioGroup currencySettingRadioGroup = (RadioGroup) settings_dialog.findViewById(R.id.rg_currency_symbol);
String[] currency_symbols_options_array = getResources().getStringArray(R.array.currency_symbols);
for ( String this_currency_option: currency_symbols_options_array ) {
RadioButton rb = new RadioButton(this);
rb.setText(this_currency_option);
currencySettingRadioGroup.addView(rb);
}