简体   繁体   English

Android单选按钮无法正常工作

[英]Android Radio Button not working properly

I am trying to make a math app for little kids. 我正在尝试为孩子们制作数学应用程序。 And there is kind of an activity to customize the settings of the math questions. 还有一种活动可以自定义数学问题的设置。 (number of digits, + or -, whether timer is enabled etc.) And I am doing the number of digits part. (数字位数,+或-,是否启用了计时器等。)而我正在做数字位数部分。 Here's my layout: 这是我的布局:

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

As you can see, I set the radio button with the text "2" to be checked by default. 如您所见,我默认设置了带有文本“ 2”的单选按钮。 However, when I run the app and click on the radio button "3", both radio button "2" and "3" is checked! 但是,当我运行该应用程序并单击单选按钮“ 3”时,将同时选中单选按钮“ 2”和“ 3”! Then I tried to click on the radio button "1" and now only radio button "1" and "2" is checked. 然后,我尝试单击单选按钮“ 1”,现在仅选中了单选按钮“ 1”和“ 2”。 In other words, whatever radio button I press, radio button "2" is always checked. 换句话说,无论我按下哪个单选按钮,总是选中单选按钮“ 2”。 But that certainly isn't how radio buttons work, right? 但这肯定不是单选按钮的工作方式,对吗?

Questions: How can I fix this behaviour without changing the fact that the the radio button "2" is checked by default? 问题:如何在不更改默认情况下选中“ 2”单选按钮的事实的情况下解决此问题? Why are they having this behaviour? 他们为什么有这种行为?

give id to your radiobutton and problem is solved 给您的单选按钮提供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"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM