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. Again for that will I have to take RadioButton or it'll be better to take RadioGroup? Than开发者_JAVA百科ks.
Put your gender RadioButton
s inside a RadioGroup
Use RadioGroup. You can see the full documentation RadioGroup
You can refer Example from developer.android.com -> http://developer.android.com/resources/tutorials/views/hello-formstuff.html#RadioButtons
For Example in your xml file :
<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;
}
}
精彩评论