从 Android 中的另一个无线电组中清除无线电组

Clear radiogroup from another radiogroup in Android

我有两个无线电组 A 和 B..

如果按下 A 中的任何一个单选按钮,则应清除 B 上选中的按钮(如果有),反之亦然..

我还需要在 selected.[=16= 中显示 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 的 select 按钮,则单选按钮不会被清除,尽管 select 值在 selected.

中正确显示

编辑:Logcat

01-13 15:35:05.886: E/AndroidRuntime(7561): FATAL EXCEPTION: main
01-13 15:35:05.886: E/AndroidRuntime(7561): java.lang.WhosebugError
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.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.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.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.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.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.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.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.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.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.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.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

我觉得一定是无限循环,因为 clearCheck 会再次触发其他 radioGroup.And 的 onCheckChanged 事件,这会导致 Whosebug 错误。

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

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

Arow.setOnCheckedChangeListener(null);
Arow.clearCheck();
Arow.setOnCheckedChangeListener(AListener);