Android Radio Button无法正常工作

时间:2015-08-22 12:15:59

标签: android radio-button android-xml

我正在尝试为小孩子制作一个数学应用程序。还有一种活动可以自定义数学问题的设置。 (位数,+或 - ,是否启用计时器等)我正在做数字部分。这是我的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context=".MainActivity"
                android:layout_gravity="center_horizontal"
                android:orientation="vertical">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/customize_menu"
        android:layout_weight="9">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/digit_text"
                android:textSize="@dimen/customize_menu_title"/>

            <RadioGroup
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1"
                    android:textSize="@dimen/customize_menu_radio"
                    android:layout_marginLeft="@dimen/radio_button_spacing"/>

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2"
                    android:textSize="@dimen/customize_menu_radio"
                    android:layout_marginLeft="@dimen/radio_button_spacing"
                    android:checked="true"/>

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="3"
                    android:textSize="@dimen/customize_menu_radio"
                    android:layout_marginLeft="@dimen/radio_button_spacing"/>

            </RadioGroup>

        </LinearLayout>

    </ScrollView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_weight="1">
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/button_trophies"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/button_start"/>
    </LinearLayout>

</LinearLayout>

如您所见,我将单选按钮设置为文本&#34; 2&#34;默认情况下要检查。但是,当我运行应用程序并单击单选按钮&#34; 3&#34;时,两个单选按钮&#34; 2&#34;和&#34; 3&#34;检查!然后我试着点击单选按钮&#34; 1&#34;现在只有单选按钮&#34; 1&#34;和&#34; 2&#34;检查。换句话说,无论我按哪个单选按钮,单选按钮&#34; 2&#34;总是被检查。但那当然不是单选按钮的工作方式,对吧?

问题:如何在不改变单选按钮&#34; 2&#34;的情况下修复此行为?默认情况下检查?为什么他们有这种行为?

1 个答案:

答案 0 :(得分:2)

为你的单选按钮提供id并解决问题

           <RadioButton android:id="@+id/btn1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1"
                android:textSize="@dimen/customize_menu_radio"
                android:layout_marginLeft="@dimen/radio_button_spacing"/>

            <RadioButton android:id="@+id/btn2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2"
                android:textSize="@dimen/customize_menu_radio"
                android:layout_marginLeft="@dimen/radio_button_spacing"
                android:checked="true"/>

            <RadioButton android:id="@+id/btn3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="3"
                android:textSize="@dimen/customize_menu_radio"
                android:layout_marginLeft="@dimen/radio_button_spacing"/>