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>
Geben Sie bitte auch Ihr Layout ein. – CoolMind
@CoolMind Layout geschrieben – ilpadrino