如何从 RadioGroup 中选择一个按钮

时间:2021-07-29 08:34:04

标签: android

如何从 RadioGroup 中选择单个项目。我分享了一个示例代码。帮助将不胜感激。

<RadioGroup
    android:id="@+id/gender"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:background="@drawable/custom_edit_text"
    android:orientation="horizontal">

    <RadioButton
        android:id="@+id/male"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/male" />

    <RadioButton
        android:id="@+id/female"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/female" />

</RadioGroup>

1 个答案:

答案 0 :(得分:0)

RadioGroup gender = findViewById(R.id.updateGender);

if(condition_for_male) {
    gender.check(R.id.male);
} else {
    gender.check(R.id.female);
}