编程创建RadioGroup的奇怪行为

时间:2017-11-05 13:31:21

标签: android radio-button radio-group android-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,并按预期检查RadioButtonv_currency_symbol变量的第一个字符匹配的文字。

Correct RadioButton selected when dialog opens

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

Clicking another option does not uncheck first

单击并选中任何其他选项会导致第二个选中的位置取消选中,但第一个RadioButton仍会被选中。

Other radio buttons behave like a separate radio group

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

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

1 个答案:

答案 0 :(得分: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); } 会导致问题。

交换两个相关的行可以解决问题。工作代码如下:

import pandas as pd 
df = pd.DataFrame(data={'col1': [1, 2, 1], 'col2': [3, 4, 5]})
print(df.loc[df['col1'] == 1])