2011-04-21 75 views
1

您好大家 我要如何驗證我的形式專門針對「性別」,在那裏我已經使用單選,這樣當「男性」選擇「女性」將自動獲得未選中。再次,我將不得不採取單選或者它會更好地利用RadioGroup中? 謝謝。如何驗證android中性別的單選按鈕?

回答

1

使用RadioGroup中內。 你可以看到完整的文檔RadioGroup

您可以從developer.android.com參考示例 - >http://developer.android.com/resources/tutorials/views/hello-formstuff.html#RadioButtons

例如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> 
0

試試這個

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; 
    } 
} 
+0

爲什麼海盜和忍者? – dove 2013-02-19 11:53:49

+0

它從官方文檔是 - [見這裏](http://developer.android.com/guide/topics/ui/controls/radiobutton.html#HandlingEvents) – 2015-05-12 10:00:18