【问题标题】:Android Radio Button not working properlyAndroid单选按钮无法正常工作
【发布时间】:2015-08-22 20:23:02
【问题描述】:

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

<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>

如您所见,我将带有文本“2”的单选按钮设置为默认选中。但是,当我运行应用程序并单击单选按钮“3”时,单选按钮“2”和“3”都被选中!然后我尝试单击单选按钮“1”,现在只选中单选按钮“1”和“2”。换句话说,无论我按下什么单选按钮,单选按钮“2”都会被选中。但这肯定不是单选按钮的工作方式,对吧?

问题:如何在不更改单选按钮“2”默认选中的事实的情况下解决此问题?他们为什么会有这种行为?

【问题讨论】:

    标签: android radio-button android-xml


    【解决方案1】:

    为您的单选按钮提供 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"/>
    

    【讨论】:

    • 有效!但是为什么我的单选按钮“1”和单选按钮“3”可以工作?
    • 只有当所有单选按钮都具有唯一标识时,radiogroup 才会起作用。
    • 我发现如果你添加一个checked="true" 并且不给除选中的其他元素之外的其他元素提供 id 将起作用,除非选中的元素不会释放其状态。
    猜你喜欢
    • 2013-11-11
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    • 2013-03-27
    • 2012-12-28
    • 1970-01-01
    相关资源
    最近更新 更多