Android性别选择器
时间: 2023-08-07 11:23:55 浏览: 48
Android性别选择器可以通过使用RadioGroup和RadioButton组合来实现。以下是一个简单的示例代码:
在XML布局文件中添加以下代码:
```
<RadioGroup
android:id="@+id/gender_radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/male_radio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"/>
<RadioButton
android:id="@+id/female_radio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"/>
</RadioGroup>
```
在Java代码中,您可以使用以下代码来获取所选的性别:
```
RadioGroup genderRadioGroup = findViewById(R.id.gender_radio_group);
int selectedId = genderRadioGroup.getCheckedRadioButtonId();
if (selectedId == R.id.male_radio_button) {
// Male selected
} else if (selectedId == R.id.female_radio_button) {
// Female selected
} else {
// No selection
}
```
当用户选择一个选项时,将返回相应的RadioButton的ID。