我有2个Radio组,每个Radio组包含2个Radiobuttons。我试图在这两组之间切换,但到目前为止都没有成功。任何帮助将不胜感激。
myGroup2 = (RadioGroup) findViewById(R.id.radioGroup2);
myGroup3 = (RadioGroup) findViewById(R.id.radioGroup3);
myGroup3.clearCheck();
myGroup2.clearCheck();
myGroup2.setOnCheckedChangeListener(this);
myGroup3.setOnCheckedChangeListener(this);
myGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
// @Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
myGroup3.clearCheck();
}
});
myGroup3.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
myGroup2.clearCheck();
}
});
答案 0 :(得分:3)
点击此处查看API:
http://developer.android.com/reference/android/widget/RadioGroup.OnCheckedChangeListener.html
编辑:
不知道花了多长时间,但我想出来了。当组已经清除时,原来onCheckedChanged仅为checkedId传递-1。如果刚刚取消选中某个方框,则仍会传递其ID。
你可以这样做:
myGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId != -1 &&
((RadioButton) findViewById(checkedId)).isChecked()){
myGroup3.clearCheck();
}
}
});
myGroup3.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId != -1 &&
((RadioButton) findViewById(checkedId)).isChecked()){
myGroup2.clearCheck();
}
}
});
或者,可能更合理一点,让活动实现监听器,这部分在onCreate方法之外:
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId != -1 && ((RadioButton) findViewById(checkedId)).isChecked()){
if(group == myGroup2){
myGroup3.clearCheck();
}
else if(group == myGroup3){
myGroup2.clearCheck();
}
// And your switch for whatever you want to happen based on the
// selected button goes here
}
}
答案 1 :(得分:1)
这是我提出的解决方案。
radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);
radioGroup2 = (RadioGroup) findViewById(R.id.radioGroup2);
RadioButton[] tempradbutton1 = {
(RadioButton) findViewById(R.id.radio1),
(RadioButton) findViewById(R.id.radio2) };
RadioButton[] tempradbutton2 = {
(RadioButton) findViewById(R.id.radio4),
(RadioButton) findViewById(R.id.radio5) };
radbutton1 = tempradbutton1;
radbutton2 = tempradbutton2;
for (int i = 0; i < radbutton1.length; i++) {
radbutton1[i].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
radioGroup2.clearCheck();
}
});
radbutton2[i].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
radioGroup1.clearCheck();
}
});
}
而不是使用 setOnCheckedChangeListener ,对于radiogroup上存在的每个单选按钮,使用 onClickListener 。
XML:
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
</RadioGroup>
<RadioGroup
android:id="@+id/radioGroup2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/radioGroup1" >
<RadioButton
android:id="@+id/radio4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radio5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
</RadioGroup>