【问题标题】:Inflating RadioGroup in Android-multiple radioButtons check at a time在Android中充气RadioGroup-一次检查多个radioButtons
【发布时间】:2013-07-15 15:51:42
【问题描述】:

我制作了一个带有可变数量单选按钮的RadioGroup。问题是可以检查许多单选按钮,而不是在RadioGroup 中检查单个RadioButton(据我所知)我的错误在哪里? 这是我的代码:

View view = inflater.inflate(R.layout.questionnaire_check_question, null);
RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup);
    for (int k = 0; k < answerValues.size(); k++) {
         View radioButtonView = inflater.inflate(R.layout.check_radio_button, null);
         RadioButton radioButton = (RadioButton) radioButtonView
                            .findViewById(R.id.radio_button);
         radioButton.setText(answerValues.get(k));
         radioGroup.addView(radioButtonView);
    }
questionBody.addView(view);

questionnaire_check_question.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:background="#A4A7A6" >

    <RadioGroup
        android:id="@+id/radioGroup"
        android:scrollbars="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    </RadioGroup>

</LinearLayout>

check_radio_button.xml

<?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/radio_button"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_marginLeft="5dp"
    android:button="@drawable/checkbox"
 >

</RadioButton>

drawable/checkbox.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:drawable="@drawable/icon_checkbox_on" android:state_checked="true"/>
    <item android:drawable="@drawable/icon_checkbox_off" android:state_checked="false"/>

</selector>

radioGroup 有一个奇怪的功能..如果我选中第一个 RadioGroup,然后是第二个,第一个就变为未选中...在此之后,如果我想再次选择第一个,我不能这样做这个..它不再检查了。 谁能帮我 ?欢迎任何想法! 提前致谢。

【问题讨论】:

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


    【解决方案1】:

    我正在举例说明这是如何完成的,然后您可以相应地修改您的代码。 在您的 xml 中,要添加单选按钮,您需要定义如下布局 -

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <RadioGroup
            android:id="@+id/radioSex"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
    
            <RadioButton
                android:id="@+id/radioMale"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/radio_male" 
                android:checked="true" />
    
            <RadioButton
                android:id="@+id/radioFemale"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/radio_female" />
    
        </RadioGroup>
    
        <Button
            android:id="@+id/btnDisplay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_display" />
    
    </LinearLayout>
    

    在您的 Java 文件中,执行以下操作: i) 像下面这样声明单选组和单选按钮 -

     private RadioGroup radioSexGroup;
      private RadioButton radioSexButton;
      private Button btn;
    

    ii)使用如下 xml 布局注册您的单选按钮和单选组 -

    radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
        btnDisplay = (Button) findViewById(R.id.btnDisplay);
    

    iii) 在按钮上添加 onclicklistner,如下所示 -

    btn.setOnClickListner(this);
    

    iv)实现 OnClickListner 接口并重写 OnClickListner 接口未实现的方法

    v) 在重写的方法 OnClick 中,编写以下代码 -

    int selectedId = radioSexGroup.getCheckedRadioButtonId();
    
                // find the radiobutton by returned id
                    radioSexButton = (RadioButton) findViewById(selectedId);
    
                Toast.makeText(MyAndroidAppActivity.this,
                    radioSexButton.getText(), Toast.LENGTH_SHORT).show();
    

    这就是它的完成方式..希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      我找到了解决方案:问题是所有按钮都具有相同的 ID。所以我为每个radioButton设置了一个唯一的id。

      【讨论】:

        猜你喜欢
        • 2012-01-06
        • 2013-06-14
        • 1970-01-01
        • 2017-01-14
        • 2011-07-03
        • 2012-06-10
        • 2018-04-17
        • 2012-05-12
        相关资源
        最近更新 更多