0

我有两个广播组 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
4

1 回答 1

2

我觉得肯定有无限循环,因为 clearCheck 将再次触发其他 radioGroup 的 onCheckChanged 事件。这会导致 stackOverflow 错误。

让我们假设有 2 个 RadioGroup A 和 B。并且您对两者都有 onCheckListener。因此,当您在 B 检查事件中调用 A.clearCheck() 时,A 的所有检查都将被删除,这会导致调用 A onCheckChanged 侦听器。在 A onCheckChanged Listener 中,您又拥有 B.clearCheck()。所以这一次又一次地重复。请试试这个,让我知道它是否有效:

Brow.setOnCheckedChangeListener(null);
Brow.clearCheck();
Brow.setOnCheckedChangeListener(BListener);

Arow.setOnCheckedChangeListener(null);
Arow.clearCheck();
Arow.setOnCheckedChangeListener(AListener);
于 2016-01-13T10:06:45.027 回答