2014-01-18 1 views
5

Wie stelle ich sicher, dass der Benutzer von jeder Funkgruppe mindestens einen Radiobutton aktiviert hat. Wie habe ich diese Radiogroup:Überprüfen Sie mindestens einen Radio-Button aus jeder Radiogruppe in Android?

<RadioGroup 
    android:id="@+id/myradio_group_one" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <RadioButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="first" 
     android:checked="true" /> 

    <RadioButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="second" /> 

    <RadioButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="third" /> 

    <RadioButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="fourt" /> 
</RadioGroup> 

<RadioGroup 
    android:id="@+id/myradio_group_two" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <RadioButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="first" 
     android:checked="true" /> 

    <RadioButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="second" /> 

    <RadioButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="third" /> 

    <RadioButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="fourt" /> 
</RadioGroup> 

Ich möchte programmgesteuert überprüfen, ob Benutzer mindestens einen Radiobutton auswählen oder nicht?

Antwort

6
RadioGroup g = (RadioGroup) findViewById(R.id.rBtnDigits); 

// Returns an integer which represents the selected radio button's ID 
int selected = g.getCheckedRadioButtonId(); 

// Gets a reference to our "selected" radio button 
RadioButton b = (RadioButton) findViewById(selected); 

// Now you can get the text or whatever you want from the "selected" radio button 
b.getText(); 
+0

Nicht von xml, muss ich dies aus programmatisch. – androidcodehunter

12

eine Referenz des RadioGroup Erhalten und dann getCheckedRadioButtonId() im Radio Gruppe anrufen, wenn nichts, dann wird der Anruf -1 zurückkehren ausgewählt ist.

Siehe public int getCheckedRadioButtonId()

2

Dies ist der Code, den ich in einem meiner Quiz App verwendet haben .. auf den Code schauen, ob dies hilft ..

private boolean checkAnswer(){ 
     String answer = getSelection(); 
     if(answer==null) { 
      Log.d("Questions", "No checkbox selected"); 
      return false; 
     } 
     else { 
     // Do your stuff here 
      return true; 
     } 

    } 


    private String getSelection(){ 


     if (c1.isChecked()) 
     { 
      return c1.getText().toString(); 
     } 
     if (c2.isChecked()) 
     { 
      return c2.getText().toString(); 
     } 
     if (c3.isChecked()) 
     { 
      return c3.getText().toString(); 
     } 
     if (c4.isChecked()) 
     { 
      return c4.getText().toString(); 
     } 

     return null; 

    } 
3
if (radioGroup.getCheckedRadioButtonId() != -1) 
{ 
    // hurray at-least on radio button is checked. 
} 
else 
{ 
    // pls select at-least one radio button.. since id is -1 means no button is check 
} 
Verwandte Themen