【发布时间】:2017-05-18 22:16:00
【问题描述】:
我想通过检查 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>
【问题讨论】:
-
请也输入您的布局。
-
@CoolMind 布局发布
标签: android radio-button radio-group oncheckedchanged