RadioGroup calls onCheckChanged() three times(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...
- 当当前选择未被选中时,
- 当新的 RadioButton 被选中时,并且
- 当 RadioGroup 保存现在检查哪个 RadioButton 时.
奇怪的怪癖.
这篇关于RadioGroup 调用 onCheckChanged() 三次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:RadioGroup 调用 onCheckChanged() 三次
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01