简体   繁体   English

如何在android中验证性别的单选按钮?

[英]How to validate Radio button for gender in android?

Hello Everybody How do I get validate my form specially for "gender", where I have used RadioButton , so that when "male" is selected "female" will automatically get unselected. 大家好我如何专门为“性别”验证我的表格,我使用了RadioButton ,这样当选择 男性”时“女性”将自动取消选中。 Again for that will I have to take RadioButton or it'll be better to take RadioGroup ? 再说一次,我必须采取RadioButton,或者最好采取RadioGroup Thanks. 谢谢。

将您的性别RadioButton放入RadioGroup

Use RadioGroup. 使用RadioGroup。 You can see the full documentation RadioGroup 你可以看到RadioGroup的完整文档

You can refer Example from developer.android.com -> http://developer.android.com/resources/tutorials/views/hello-formstuff.html#RadioButtons 您可以参考developer.android.com上的示例 - > http://developer.android.com/resources/tutorials/views/hello-formstuff.html#RadioButtons

For Example in your xml file : 对于xml文件中的示例:

 <RadioGroup android:layout_height="wrap_content" android:id="@+id/gender_group"
    android:layout_width="match_parent">
    <RadioButton android:text="Male"
        android:layout_width="wrap_content" android:id="@+id/radio0"
        android:layout_height="wrap_content" android:checked="true"></RadioButton>
    <RadioButton android:text="Female"
        android:layout_width="wrap_content" android:id="@+id/radio1"
        android:layout_height="wrap_content"></RadioButton>
</RadioGroup>

try this 尝试这个

public void onRadioButtonClicked(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.radio_pirates:
            if (checked)
                // Pirates are the best
            break;
        case R.id.radio_ninjas:
            if (checked)
                // Ninjas rule
            break;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM