2017-12-01 4 views
-3

Ich habe eine MCQs App mit vier Radio-Buttons in einer Radio-Gruppe erstellt. Problem ist, dass ich einen Radioknopf auswählen und andere drei Radioknopfauswahl zu dieser Zeit deaktivieren möchte. Nur eine Optionsschaltfläche aktiviert und deaktiviert die Auswahl anderer Optionsfelder. Bitte helfen Sie mir aus: Ich werde Ihnen allen sehr dankbar sein.Kann nicht mit Radio-Tasten-Auswahl umgehen

---------- 

Xml File 

<RadioGroup 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/radioGroup1"> 
//These are four radio buttons from which i have to select one radio button and selection of other three radio buttons must be disabled. 
     <RadioButton 
      android:id="@+id/choice1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="5dp" 
      android:background="@android:color/darker_gray" 
      android:gravity="left|center_vertical" 
      android:onClick="onClick" 
      android:padding="4dp" 
      android:text="A" 
      android:textColor="#000000" 
      android:textSize="16dp" /> 

    <RadioButton 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="B" 
     android:onClick="onClick" 
     android:background="@android:color/darker_gray" 
     android:textColor="#000000" 
     android:padding="4dp" 
     android:textSize="16dp" 
     android:gravity="left|center_vertical" 
     android:layout_marginBottom="5dp" 
     android:id="@+id/choice2"/> 

    <RadioButton 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="C" 
     android:onClick="onClick" 
     android:background="@android:color/darker_gray" 
     android:textColor="#000000" 
     android:padding="4dp" 
     android:textSize="16dp" 
     android:gravity="left|center_vertical" 
     android:layout_marginBottom="5dp" 
     android:id="@+id/choice3"/> 

    <RadioButton 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="D" 
     android:onClick="onClick" 
     android:background="@android:color/darker_gray" 
     android:textColor="#000000" 
     android:padding="4dp" 
     android:textSize="16dp" 
     android:gravity="left|center_vertical" 
     android:layout_marginBottom="5dp" 
     android:id="@+id/choice4" /> 
</RadioGroup> 

---------- 

Java File: 
mQuestionView = (TextView) findViewById(R.id.question); 
     mButtonChoice1 = (RadioButton) findViewById(R.id.choice1); 
     mButtonChoice2 = (RadioButton) findViewById(R.id.choice2); 
     mButtonChoice3 = (RadioButton) findViewById(R.id.choice3); 
     mButtonChoice4 = (RadioButton) findViewById(R.id.choice4); 
} 
    //Java file with only onclick button code: 
public void onClick(View view) { 
      Button answer = (Button) view; 
      // Is the button now checked? 
      //here must be the code of my problem 
     } 

Antwort

0

Zum Deaktivieren von RadioGroup den folgenden Code versuchen. Wenn es nicht funktioniert, versuche es zu loopen.

RadioGroup radioGroup = (RadioGroup)findViewById (R.id.radioGroup1); 
radioGroup.setOnCheckedChangedListener (new OnCheckedChangedListener(){ 
public void onCheckedChanged(RadioGroup group, int checkedId) 
{ 
    if (checkedId == R.id.choice1) 
    group.setEnabled(false); 
}else if(/*other conditions here*/) 
}); 

Wenn setEnabled() nicht funktioniert, so sollten Sie versuchen, den Code zu ändern, so dass es loops through each RadioButton beim Check radio0:

if (checkedId == R.id.choice1){ 

    //radio0 is checked save the data 

    for(int i = 0; i < group.getChildCount(); i++){ 
      ((RadioButton)rg1.getChildAt(i)).setEnabled(false); 
     } 
}else if(/*other conditions here*/){ 

     for(int i = 0; i < group.getChildCount(); i++){ 
      ((RadioButton)rg1.getChildAt(i)).setEnabled(false); 
     } 
} 
+0

was habe ich hier? if (checkedId == R.id.radio0) Radio0 ist nicht in meinem Code? –

+0

radio0 ist Ihre Wahl1; Ich hatte den Code geändert. überprüfen Sie es einfach –

+0

Ich muss diesen Code mit allen vier Möglichkeiten, choice1, choice2, choice3 und choice4 getrennt machen? –

0

Im nicht ganz sicher, was Sie fordern, aber ich denke, das ist eine Lösung:

public void onClick(View view) 
{ 
    ArrayList<Integer> ids = new ArrayList<Integer>(); 
    ids.add(mButtonChoice1.getId()); 
    ids.add(mButtonChoice2.getId()); 
    ids.add(mButtonChoice3.getId()); 
    ids.add(mButtonChoice4.getId()); 
    for(Integer i : ids) 
    { 
     if(i!= view.getId()) 
      ((RadioButton) findViewById(i)).setEnabled(false); 
    } 
} 
+0

Ich weiß, es ist beschissen, aber könnte funktionieren –

+0

Es funktioniert, aber wenn ich auf die nächste Frage klicke, werden die Optionsfelder nicht zurückgesetzt, es behält seine Änderungen von vorherigen MCQ Frage. –

+0

was tun? –

Verwandte Themen