我有两个广播组 A 和 B..
如果按下 A 中的任何一个单选按钮,则应清除 B 上的选中按钮(如果有),反之亦然。
我还需要在selected中显示select的值。
我已经实现了这样的东西。
private EditText selected;
private int select = 0;
private RadioGroup Arow;
private RadioGroup Brow;
实施 findViewById 后..
Arow.setOnCheckedChangeListener(AListener);
Brow.setOnCheckedChangeListener(BListener);
Arow 的听众:
OnCheckedChangeListener AListener= new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.one:
select = 1;
break;
case R.id.two:
select = 2;
break;
case R.id.three:
select = 3;
break;
default:
break;
}
if(select > 0){
selected.setText(""+ select );
}
Arow.clearCheck(); // point 1
}
};
眉毛的听众:
OnCheckedChangeListener BListener= new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.four:
select = 4;
break;
case R.id.five:
select = 5;
break;
case R.id.six:
select = 6;
break;
default:
break;
}
if( select > 0){
selected.setText(""+ select );
}
Brow.clearCheck(); // point 2
}
};
问题是上面的代码崩溃了,如果我评论上面的第 1 点和第 2 点,代码可以工作,但是如果我碰巧从 A 和 B 中选择按钮,单选按钮不会被清除,尽管选择值正确显示在选定的。
编辑:Logcat
01-13 15:35:05.886: E/AndroidRuntime(7561): FATAL EXCEPTION: main
01-13 15:35:05.886: E/AndroidRuntime(7561): java.lang.StackOverflowError
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.text.SpannableStringBuilder.getChars(SpannableStringBuilder.java:913)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.text.TextUtils.getChars(TextUtils.java:70)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.text.method.ReplacementTransformationMethod$ReplacementCharSequence.getChars(ReplacementTransformationMethod.java:151)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.text.TextUtils.getChars(TextUtils.java:70)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.text.TextUtils.indexOf(TextUtils.java:103)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.text.StaticLayout.generate(StaticLayout.java:182)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.text.DynamicLayout.reflow(DynamicLayout.java:332)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.text.DynamicLayout.<init>(DynamicLayout.java:176)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.TextView.makeSingleLayout(TextView.java:6263)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.TextView.makeNewLayout(TextView.java:6161)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.TextView.checkForRelayout(TextView.java:6708)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.TextView.setText(TextView.java:3783)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.TextView.setText(TextView.java:3641)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.EditText.setText(EditText.java:94)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.TextView.setText(TextView.java:3616)
01-13 15:35:05.886: E/AndroidRuntime(7561): at com.xerox.ts.ops.hsd.activity.DemoActivity$2.onCheckedChanged(DemoActivity.java:232)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.RadioGroup.setCheckedId(RadioGroup.java:174)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.RadioGroup.check(RadioGroup.java:168)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.RadioGroup.clearCheck(RadioGroup.java:209)
01-13 15:35:05.886: E/AndroidRuntime(7561): at com.xerox.ts.ops.hsd.activity.DemoActivity$1.onCheckedChanged(DemoActivity.java:203)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.RadioGroup.setCheckedId(RadioGroup.java:174)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.RadioGroup.check(RadioGroup.java:168)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.RadioGroup.clearCheck(RadioGroup.java:209)
01-13 15:35:05.886: E/AndroidRuntime(7561): at com.xerox.ts.ops.hsd.activity.DemoActivity$2.onCheckedChanged(DemoActivity.java:234)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.RadioGroup.setCheckedId(RadioGroup.java:174)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.RadioGroup.check(RadioGroup.java:168)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.RadioGroup.clearCheck(RadioGroup.java:209)
01-13 15:35:05.886: E/AndroidRuntime(7561): at com.xerox.ts.ops.hsd.activity.DemoActivity$1.onCheckedChanged(DemoActivity.java:203)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.RadioGroup.setCheckedId(RadioGroup.java:174)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.RadioGroup.check(RadioGroup.java:168)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.RadioGroup.clearCheck(RadioGroup.java:209)
01-13 15:35:05.886: E/AndroidRuntime(7561): at com.xerox.ts.ops.hsd.activity.DemoActivity$2.onCheckedChanged(DemoActivity.java:234)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.RadioGroup.setCheckedId(RadioGroup.java:174)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.RadioGroup.check(RadioGroup.java:168)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.RadioGroup.clearCheck(RadioGroup.java:209)
01-13 15:35:05.886: E/AndroidRuntime(7561): at com.xerox.ts.ops.hsd.activity.DemoActivity$1.onCheckedChanged(DemoActivity.java:203)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.RadioGroup.setCheckedId(RadioGroup.java:174)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.RadioGroup.check(RadioGroup.java:168)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.RadioGroup.clearCheck(RadioGroup.java:209)
01-13 15:35:05.886: E/AndroidRuntime(7561): at com.xerox.ts.ops.hsd.activity.DemoActivity$2.onCheckedChanged(DemoActivity.java:234)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.RadioGroup.setCheckedId(RadioGroup.java:174)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.RadioGroup.check(RadioGroup.java:168)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.RadioGroup.clearCheck(RadioGroup.java:209)
01-13 15:35:05.886: E/AndroidRuntime(7561): at com.xerox.ts.ops.hsd.activity.DemoActivity$1.onCheckedChanged(DemoActivity.java:203)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.RadioGroup.setCheckedId(RadioGroup.java:174)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.RadioGroup.check(RadioGroup.java:168)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.RadioGroup.clearCheck(RadioGroup.java:209)
01-13 15:35:05.886: E/AndroidRuntime(7561): at com.xerox.ts.ops.hsd.activity.DemoActivity$2.onCheckedChanged(DemoActivity.java:234)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.RadioGroup.setCheckedId(RadioGroup.java:174)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.RadioGroup.check(RadioGroup.java:168)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.RadioGroup.clearCheck(RadioGroup.java:209)
01-13 15:35:05.886: E/AndroidRuntime(7561): at com.xerox.ts.ops.hsd.activity.DemoActivity$1.onCheckedChanged(DemoActivity.java:203)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.RadioGroup.setCheckedId(RadioGroup.java:174)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.RadioGroup.check(RadioGroup.java:168)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.RadioGroup.clearCheck(RadioGroup.java:209)
01-13 15:35:05.886: E/AndroidRuntime(7561): at com.xerox.ts.ops.hsd.activity.DemoActivity$2.onCheckedChanged(DemoActivity.java:234)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.RadioGroup.setCheckedId(RadioGroup.java:174)
01-13 15:35:05.886: E/AndroidRuntime(7561): at android.widget.RadioG