我有两个广播组A和B ..
如果按下A中的任何一个单选按钮,则应清除B上的选中按钮(如果有),反之亦然..
此外,我需要在选择中显示选择的值。
我已经实现了类似的东西。
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
}
};
Brow的听众:
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
答案 0 :(得分:1)
我觉得必须有无限循环,因为clearCheck会再次触发其他radioGroup的onCheckChanged事件。这会导致stackOverflow错误。
假设有2个RadioGroup A和B.并且你有两个onCheckListener。因此,当您在B检查事件中调用A.clearCheck()时,A的所有检查都将被删除,从而导致调用A onCheckChanged Listener。在A onCheckChanged Listener中,你再次拥有B.clearCheck()。所以这一再重演。 请尝试这个,让我知道它是否有效:
Brow.setOnCheckedChangeListener(null);
Brow.clearCheck();
Brow.setOnCheckedChangeListener(BListener);
Arow.setOnCheckedChangeListener(null);
Arow.clearCheck();
Arow.setOnCheckedChangeListener(AListener);