2016-05-19 10 views
7

Ich habe einen Radioknopf in Radiogroup erstellt, aber wenn ich versuche, die Apps auszuführen, können alle Radioknöpfe ständig ausgewählt werden, und wie man nur einen setzt Radiobutton kann gleichzeitig ausgewählt werden?Wie man nur einen RadioButton einstellt Kann zu der Zeit in RadioGroup ausgewählt werden

Ich verwende Fragment

RadioGroup radioGroup = (RadioGroup) rootView.findViewById(R.id.RGroup); 
     radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(RadioGroup group, int checkedId) { 
       // find which radio button is selected 
       if(checkedId == R.id.Abdominal) { 
        Toast.makeText(getActivity().getApplicationContext(), "choice: A", 
          Toast.LENGTH_SHORT).show(); 
       } else if(checkedId == R.id.Arm) { 
        Toast.makeText(getActivity().getApplicationContext(), "choice: B", 
          Toast.LENGTH_SHORT).show(); 
       } else if(checkedId == R.id.Back){ 
        Toast.makeText(getActivity().getApplicationContext(), "choice: C", 
          Toast.LENGTH_SHORT).show(); 
       } else if(checkedId == R.id.Chest){ 
        Toast.makeText(getActivity().getApplicationContext(), "choice: D", 
          Toast.LENGTH_SHORT).show(); 
       } else if(checkedId == R.id.Leg){ 
        Toast.makeText(getActivity().getApplicationContext(), "choice: E", 
          Toast.LENGTH_SHORT).show(); 
       } else if(checkedId == R.id.Shoulder){ 
        Toast.makeText(getActivity().getApplicationContext(), "choice: F", 
          Toast.LENGTH_SHORT).show(); 
       } 
      } 

     }); 

hier mein xml-Code für RG und RB

<RadioGroup 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:id="@+id/RGroup"> 

        <TableRow android:weightSum="1"> 
        <RadioButton 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="Abdominal" 
         android:id="@+id/Abdominal"/> 
        <RadioButton 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="Arm" 
         android:id="@+id/Arm"/> 
        <RadioButton 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="Back" 
         android:id="@+id/Back" /> 
         </TableRow> 
        <TableRow> 
         <RadioButton 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="Chest" 
          android:id="@+id/Chest"/> 
         <RadioButton 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="Leg" 
          android:id="@+id/Leg"/> 
         <RadioButton 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="Shoulder" 
          android:id="@+id/Shoulder"/> 
        </TableRow> 
       </RadioGroup> 

Edited 1: Antwort: Wenn Sie nicht wollen, Radio-Button ausgewählt werden in einer Zeit, so verwenden Sie nicht Charerow

Antwort

11

Es funktioniert nicht, weil der TableRow innen Radiogroup zu tun. Alle RadioButtons sind aufgrund von TableRow zwischen ihnen nicht gruppiert.

RadioButton sollte das direkte Kind von RadioGroup sein, andernfalls Gruppierung funktioniert nicht.

einfach Ihren Code wie folgt ändern, es wird funktionieren:

 <RadioGroup 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/RGroup"> 

      <RadioButton 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Abdominal" 
       android:id="@+id/Abdominal"/> 
      <RadioButton 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Arm" 
       android:id="@+id/Arm"/> 
      <RadioButton 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Back" 
       android:id="@+id/Back" />           

      <RadioButton 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Chest" 
       android:id="@+id/Chest"/> 
      <RadioButton 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Leg" 
       android:id="@+id/Leg"/> 
      <RadioButton 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Shoulder" 
       android:id="@+id/Shoulder"/> 

     </RadioGroup> 

Hoffnung, das hilft. :)

+0

, Wow Dank! Dies ist die Antwort, alle Taste kann einmal bc von tablerow ausgewählt werden :) Danke für Hilfe –

+0

Btw, ich benutze TableRow, um einen Radiobutton in 2 Zeilen zu machen, so wie es 2rwos ohne tablerow machen? –

+0

lass mich sehen .. ich werde das bald aktualisieren .. !! –

5
<RadioGroup 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:id="@+id/radioGroup"> 
     <RadioButton 
      android:layout_width="0dp" 
      android:layout_weight="50" 
      android:layout_height="wrap_content" 
      android:text="Debit" 
      android:id="@+id/rDebit" 
      android:checked="false" 
      /> 

     <RadioButton 
      android:layout_width="0dp" 
      android:layout_weight="50" 
      android:layout_height="wrap_content" 
      android:text="Credit" 
      android:id="@+id/rCredit" 
      android:checked="false" /> 

    </RadioGroup> 

Und in Java-Datei

RadioGroup radioGroup; 



radioGroup = (RadioGroup) findViewById(R.id.radioGroup); 

Und wenn etwas

if (radioGroup.getCheckedRadioButtonId() == R.id.rCredit) 
{ 
// do something 
} 
+0

Code, wobei jedoch alle Radiobutton können immer noch in einer Zeit ausgewählt werden, im btw mit Fragmente –

2

Einfacher Weg. Klicken Sie auf den Radio-Button. Code wie unten beschrieben eingeben.

public void clearRadioChecked() { 
    rdopa.setChecked(false); 
    rdopb.setChecked(false); 
    rdopc.setChecked(false); 
    rdopd.setChecked(false); 
} 

Wenn Sie rdopa dann auf Klicken von rdopa tun möchten, tun Sie unten.

clearRadioChecked() 
rdopa.setChecked(true); 
+0

Wir verwenden besser ** CheckBoxen ** dann .. Sie verwenden RadioButton nicht, um es manuell zu aktivieren und zu deaktivieren. Das Standardverhalten ist ** eine kann auf einmal überprüft werden, andere wird automatisch deaktiviert **. –

+0

@ janki gadhiya, wenn Sie eine Gruppe von Radiobutton haben und Sie für viele Zeit wiederverwenden möchten, dann müssen Sie oben Weg implementieren. Sonst haben Sie einzelne Gruppe von Radio-Taste, dann funktioniert es gut nach Ihrem Konzept automatisch check/uncheck. – Vasant