<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="horizontal"
android:checkedButton="@+id/rb_male" >
<RadioButton
android:id="@+id/rb_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:textColor="@color/black" />
<RadioButton
android:id="@+id/rb_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="女"
android:textColor="@color/black" />
</RadioGroup>
以上示例可以完成一个单选框,注意的是RadioGroup中的属性android:checkedButton="@+id/rb_male",这个是指默认选中的按钮。如果不在这里设置,直接在下边的某个RadioButton中设置属性android:checked="true",这种情况下,在运行时,两个RadioButton都可以被选中。

本文介绍如何使用RadioGroup和RadioButton组件创建一个默认选中项的单选框。注意RadioGroup的checkedButton属性设置,默认选中ID为rb_male的RadioButton。
1326

被折叠的 条评论
为什么被折叠?



