2017-05-18 13 views
0

Ich möchte einen Wert von einem RadioButton überprüfen. Ich habe drei RadioButton (mit verschiedenen Werten) in einer RadioGroup. Nun, das erste Mal, wenn ich einen RadioButton überprüfe, kann ich den Wert erhalten, aber wenn ich auf einen anderen RadioButton umschalte, kann ich keine Werte mehr bekommen. Es ist so, als ob onCheckedChanged nur beim ersten Aufruf eines RadioButtons angerufen wird.OnCheckChanged wird nur das erste Mal

Jede hilfreiche wird geschätzt. Vielen Dank im Voraus.

Dies ist mein Code:

public class AsesorNutri extends AppCompatActivity { 


RadioGroup deporte; 
RadioButton andar; 
RadioButton correr; 
RadioButton ciclismo; 
String TAG=""; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_asesor_nutri); 
    andar=(RadioButton)findViewById(R.id.andar); 
    andar.setChecked(true); 
    correr=(RadioButton)findViewById(R.id.correr); 
    ciclismo=(RadioButton)findViewById(R.id.ciclismo); 
    deporte = (RadioGroup)findViewById(R.id.deporte); 


    deporte.setOnCheckedChangeListener(cambiodeporte); 

} 

    public RadioGroup.OnCheckedChangeListener cambiodeporte=new RadioGroup.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) { 
      switch(i) 
      { 
       case R.id.andar: 
        Log.i(TAG, "cambia andar"); 

        // do what you wan there 
        break; 
       case R.id.correr: 
        Log.i(TAG, "cambia correr"); 

        break; 

       case R.id.ciclismo: 
        Log.i(TAG, "cambia correr"); 


        break; 
      } 
     } 
    }; 



} 

Und das ist mein Layout:

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/MainActivity" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:clickable="true" 

tools:context="lnspad.fitnessup.AsesorNutri"> 


<TextView 
    android:id="@+id/textView3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginStart="24dp" 
    android:layout_marginTop="67dp" 
    android:text="Seleccione Deporte" 
    android:textSize="18sp" /> 

<RadioGroup 
    android:id="@+id/deporte" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginStart="36dp" 
    android:layout_marginTop="18dp" 
    android:layout_below="@+id/textView3" 
    android:layout_alignStart="@+id/textView3"> 

    <RadioButton 
     android:id="@+id/andar" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignStart="@+id/textView3" 
     android:layout_alignTop="@+id/deporte" 
     android:text="Andar" /> 



<RadioButton 
    android:id="@+id/correr" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Correr" 
    android:layout_marginEnd="39dp" 
    android:layout_alignBaseline="@+id/ciclismo" 
    android:layout_alignBottom="@+id/ciclismo" 
    android:layout_toStartOf="@+id/ciclismo" /> 

<RadioButton 
    android:id="@+id/ciclismo" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Ciclismo" 
    android:layout_below="@+id/deporte" 
    android:layout_alignParentEnd="true" 
    android:layout_marginEnd="49dp" /> 


    </RadioGroup> 

    </RelativeLayout> 
+0

Geben Sie bitte auch Ihr Layout ein. – CoolMind

+0

@CoolMind Layout geschrieben – ilpadrino

Antwort

0

Ihr Code ungerade ist. Nicht sicher, warum Sie einen öffentlichen Zugriffsmodifikator für den Listener haben. Warum definieren Sie es nicht genauso wie die anderen Variablen? Die Anmerkung behauptet, es sei eine Ressourcen-ID, und sie ist konstant, also muss sie funktionieren. Andernfalls könnte es sich um ein Lebenszyklusproblem handeln, da Sie in onCreate() initialisiert werden.

Beispiel:

RadioGroup deporte; 
RadioButton andar; 
RadioButton correr; 
RadioButton ciclismo; 
RadioGroup.OnCheckedChangeListener cambiodeporte; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_asesor_nutri); 
    andar=(RadioButton)findViewById(R.id.andar); 
    andar.setChecked(true); 
    correr=(RadioButton)findViewById(R.id.correr); 
    ciclismo=(RadioButton)findViewById(R.id.ciclismo); 
    deporte = (RadioGroup)findViewById(R.id.deporte); 
    cambiodeporte = new RadioGroup.OnCheckedChangeListener() { 
    @Override 
    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) { 
     switch(i) 
     { 
      case R.id.andar: 
       Log.i(TAG, "cambia andar"); 

       // do what you wan there 
       break; 
      case R.id.correr: 
       Log.i(TAG, "cambia correr"); 

       break; 

      case R.id.ciclismo: 
       Log.i(TAG, "cambia correr"); 


       break; 
      } 
     } 
    }; 

    deporte.setOnCheckedChangeListener(cambiodeporte); 
} 
+0

@ eine Person Es funktioniert nicht. Gleiches Verhalten. Ich weiß nicht, ob die Tatsache, dass diese Aktivität von MainActivity gestartet wird, irgendeine Beziehung zu diesem Problem hat. – ilpadrino

0

Sie Code funktioniert. Ich habe mich ein bisschen verändert und Toasts gezeigt.

private RadioGroup.OnCheckedChangeListener cambiodeporte=new RadioGroup.OnCheckedChangeListener() { 
    @Override 
    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) { 
     switch (i) { 
      case R.id.andar: 
       Log.i(TAG, "cambia andar"); 
       Toast.makeText(MainActivity.this, "1. cambia andar", Toast.LENGTH_SHORT).show(); 

       break; 
      case R.id.correr: 
       Log.i(TAG, "cambia correr"); 
       Toast.makeText(MainActivity.this, "2. cambia correr", Toast.LENGTH_SHORT).show(); 

       break; 

      case R.id.ciclismo: 
       Log.i(TAG, "cambia ciclismo"); 
       Toast.makeText(MainActivity.this, "3. cambia ciclismo", Toast.LENGTH_SHORT).show(); 

       break; 
     } 
    } 
}; 

Vielleicht ist es ein Geräteproblem.

+0

Ja ... es funktioniert. Ich denke, das Problem war, dass das Log nicht funktioniert. Das war der Grund, warum ich geantwortet habe, dass es nicht funktionierte. Toast zeigt, dass der Code funktioniert. Danke für deine Antwort. – ilpadrino

+0

@ilpadrino, habe ich auch bemerkt, dass Logs falsche Werte enthielten ("cambia correr" in zwei Fällen). Also änderte ich Logs und fügte Toast hinzu. – CoolMind

Verwandte Themen