RadioGroup 调用 onCheckChanged() 三次

RadioGroup calls onCheckChanged() three times(RadioGroup 调用 onCheckChanged() 三次)
本文介绍了RadioGroup 调用 onCheckChanged() 三次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力解决 RadioButton 的编程检查问题,该 RadioButton 位于 RadioGroup 中.似乎单个 check() 调用会引发三个事件 - 第一个 RadioButton 一次,第二个 RadioButton 两次,这是我的目标.同时点击界面中的第二个RadioButton,只会出现一个事件,没错.

I've been struggling with an issue of programmatic checking of a RadioButton, which is situated in a RadioGroup. It seems that a single check() call raises three events - once for the first RadioButton and twice for the second, which is my target. At the same time clicking on the second RadioButton in the interface causes only one event to appear, which is correct.

那么,有没有办法避免多个事件引发?

So, is there a way to avoid multiple event raising ?

public class RadiogroupActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        RadioGroup r = (RadioGroup) findViewById(R.id.radioGroup1);
        RadioButton b = (RadioButton) findViewById(R.id.radio1);
        r.setOnCheckedChangeListener(onPromobuttonsClick);
        r.check(R.id.radio1);

    }

    private OnCheckedChangeListener onPromobuttonsClick = new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            Log.d("x", "x");
        }
    };
}

推荐答案

那么,有没有办法避免多个事件引发?

So, is there a way to avoid multiple event raising ?

调用 b.setChecked(true); 代替.

快速浏览 源代码确认 RadioGroup#check() 确实确实调用了 OnCheckChangedListener 三次...

A quick look through the source code confirms that RadioGroup#check() really does call the OnCheckChangedListener three times...

  1. 当当前选择未被选中时,
  2. 当新的 RadioButton 被选中时,并且
  3. 当 RadioGroup 保存现在检查哪个 RadioButton 时.

奇怪的怪癖.

这篇关于RadioGroup 调用 onCheckChanged() 三次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)