gpt4 book ai didi

android - RadioGroup 调用 onCheckChanged() 三次

转载 作者:IT老高 更新时间:2023-10-28 23:03:35 25 4
gpt4 key购买 nike

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

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

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); 代替。


快速浏览source code确认 RadioGroup#check() 确实确实调用了 OnCheckChangedListener 三次...

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

奇怪的怪癖。

关于android - RadioGroup 调用 onCheckChanged() 三次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10263778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com