Android单选按钮工作不正常
我正在尝试为小孩子制作一个数学应用程序。还有一种活动可以自定义数学问题的设置。(位数,+或-,定时器是否启用等),我正在做位数部分。以下是我的布局:Android单选按钮工作不正常,android,radio-button,android-xml,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
<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”默认选中的情况下修复此行为?他们为什么会有这种行为?给你的单选按钮提供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"/>
给你的单选按钮提供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"/>
给你的单选按钮提供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"/>
给你的单选按钮提供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,则该元素将起作用,但选中的元素不会释放其状态。它起作用!但是为什么我的单选按钮“1”和单选按钮“3”起作用呢?只有当所有单选按钮都具有唯一标识时,radiogroup才会起作用。我发现,如果您添加一个checked=“true”
,并且不给除选中之外的其他元素提供ID,则该元素将起作用,但选中的元素不会释放其状态。它起作用!但是为什么我的单选按钮“1”和单选按钮“3”起作用呢?只有当所有单选按钮都具有唯一标识时,radiogroup才会起作用。我发现,如果您添加一个checked=“true”
,并且不给除选中之外的其他元素提供ID,则该元素将起作用,但选中的元素不会释放其状态。它起作用!但是为什么我的单选按钮“1”和单选按钮“3”起作用呢?只有当所有单选按钮都具有唯一标识时,radiogroup才会起作用。我发现,如果您添加一个checked=“true”
并且不给除选中之外的其他元素提供ID,则该元素将起作用,但选中的元素不会释放其状态。