我有 2 个活动可以来回导航。两者都有一个无线电组。问题是第一个活动的无线电组 ID 为 :1、2 、3 、4,第二个活动的 id 为 :5、6、7、8。但是当我再次切换回第一个活动时,ID 更改为:9、10、11、12。有解决方案吗?
以下是我如何定义我的 grouop:
<RadioGroup
android:id="@+id/leftRadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
android:layout_alignParentBottom="true"
android:layout_marginLeft="150dp"
android:layout_marginBottom="10dp">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LED OFF"
android:layout_margin="7dp"
android:textSize="10sp"
android:fontFamily="serif"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Position LED"
android:layout_margin="7dp"
android:textSize="10sp"
android:fontFamily="serif"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Force LED"
android:layout_margin="7dp"
android:textSize="10sp"
android:fontFamily="serif"
/>
</RadioGroup>
这是我在它们上设置单击侦听器的方法:
leftRadio = (RadioGroup)findViewById(R.id.leftRadioGroup);
leftRadio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
Toast.makeText(getApplicationContext(),""+i,Toast.LENGTH_SHORT).show();
if(i==1) {
leftLed = 0;
}
else if(i==2) {
leftLed = 1;
}
else {
leftLed=3;
}
}
});
您应该在RadioButton
中添加android:id
属性,并在onCheckedChanged()
方法中检查此id
。
例:
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
在RadioGroup.setOncheckedChangedListener
:
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
Toast.makeText(getApplicationContext(),""+i,Toast.LENGTH_SHORT).show();
if(i==R.id.radioButton1) {
leftLed = 0;
}
else if(i==R.id.radioButton2) {
leftLed = 1;
}
else {
leftLed=3;
}
}
您可能希望查看 Android RadioGroup 示例