使用Android studio的广播组中的一个问题:java和xml

问题描述 投票:0回答:1

创建单选组时,我在其中插入了 3 个单选按钮,并且在执行代码并检查第二个单选按钮时默认选中第一个按钮,例如第一个按钮仍处于选中状态。

<RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/radg">

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/agimmob"
                android:textColor="@color/welcomecolor"
                android:textSize="20dp"
                android:layout_marginLeft="45dp"
                />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/prop"
                android:textColor="@color/welcomecolor"
                android:textSize="20dp"
                android:layout_marginLeft="45dp"
                android:checked="true"

                />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/achet"
                android:textColor="@color/welcomecolor"
                android:textSize="20dp"
                android:layout_marginLeft="45dp"/>

        </RadioGroup>

我尝试默认检查第二个,但我遇到了同样的问题

java android android-radiobutton
1个回答
0
投票

您必须使用 RadioGroup 组件中的 checkedButton 属性,并将默认情况下要检查的 radioButton 的 id 传递给该属性

<RadioGroup
    android:id="@+id/radg"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:checkedButton="@id/rb1">

没有必要使用radioRuttons上的checked属性

© www.soinside.com 2019 - 2024. All rights reserved.