RadioGroup中调用onCheckChanged()三次 [英] RadioGroup calls onCheckChanged() three times

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

问题描述

我一直挣扎着一个单选按钮,这是坐落在一个RadioGroup中的纲领性检查的问题。看来,一个检查()调用产生三个事件 - 一次,第一次单选按钮,两次第二,这是我的目标。同时在界面点击第二单选按钮导致显示只有一个事件,这是正确的。

那么,有没有办法避免多个事件认识?

 公共类RadiogroupActivity延伸活动{
    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        RadioGroup中R =(RadioGroup中)findViewById(R.id.radioGroup1);
        单选B =(单选)findViewById(R.id.radio1);
        r.setOnCheckedChangeListener(onPromobuttonsClick);
        r.check(R.id.radio1);

    }

    私人OnCheckedChangeListener onPromobuttonsClick =新OnCheckedChangeListener(){
        @覆盖
        公共无效onCheckedChanged(RadioGroup中组,诠释checkedId){
            Log.d(×,×);
        }
    };
}
 

解决方案
  

那么,有没有办法避免多个事件认识?

呼叫 b.setChecked(真); 而不是


有一个快速浏览一下通过<一个href="http://www.grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.1.1_r1/android/widget/RadioGroup.java#RadioGroup.check%28int%29">source code 确认 RadioGroup中#检查()确实叫OnCheckChangedListener三次......

  1. 当当前选择未被选中,
  2. 当新的单选按钮被选中,和
  3. 当RadioGroup中保存该单选现在检查。

奇夸克。

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 ?

Call b.setChecked(true); instead.


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

  1. When the current selection is unchecked,
  2. When the new RadioButton is checked, and
  3. When the RadioGroup saves which RadioButton is now checked.

Odd quirk.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆