【问题标题】:Strange behaviour of programatically created RadioGroup以编程方式创建的 RadioGroup 的奇怪行为
【发布时间】:2025-12-12 06:00:01
【问题描述】:

我正在使用以下代码以编程方式将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 变量的第一个字符匹配的文本按预期进行检查。

但是,单击任何其他 RadioButtons 不会导致选中的选项取消选中 - 我最终选中了两个选项。

单击并选中任何其他选项会导致第二个选中位置取消选中,但第一个RadioButton保持选中状态。

几乎就像以编程方式检查的RadioButton 属于一个单独的 RadioGroup。

删除在创建时检查RadioButtons 之一的两行允许RadioGroup 正常运行,但您显然无法看到之前的选择。

【问题讨论】:

  • 您是否调试过您的代码以了解问题出在哪里?顺便说一句,None 单选按钮应该是您将其分开的 radioGroup 之一?**You added two radio button to the same radioGroup which mean that you will have two checked buttons**跨度>
  • 除了卡住的按钮外,一切似乎都正常工作。 RadioButtons 上的 onClick 事件被正确触发等以正确设置 v_currency_symbol 值,当对话框关闭并重新打开时,正确的 me 值显示为已选中,但随后这个值被卡住了。 None 选项在 RadioGroup 内,它不是从 for 循环读取的数组中创建的。

标签: android radio-button radio-group android-radiogroup


【解决方案1】:

我发现了问题...检查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);
    }

【讨论】: