我想从检查RadioButton中获得一个值。我在RadioGroup中有三个RadioButton (具有不同的值)。嗯,当我第一次检查一个RadioButton时,我可以得到它的值,但是如果我换成另一个RadioButton,我就不能再得到值了。这就像我第一次检查RadioButton时只调用onCheckedChanged一样。
任何有帮助的人都将不胜感激。提前谢谢。
这是我的代码:
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;
}
}
};
}这是我的布局:
<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>发布于 2017-05-19 06:31:05
你的代码很奇怪。不确定为什么在监听程序上有public访问修饰符。为什么不像定义其他变量一样定义它呢?注释声明它是一个资源ID,并且它是常量,所以它必须工作。否则,这可能是一个生命周期问题,因为您是在onCreate()中进行初始化。
例如:
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);
}发布于 2017-05-19 16:40:11
你的代码可以工作了。我稍微改变了一下,并展示了敬酒词。
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;
}
}
};也许是设备问题。
https://stackoverflow.com/questions/44058609
复制相似问题