【问题标题】:RadioGroup calls onCheckChanged() three timesRadioGroup 调用 onCheckChanged() 三次
【发布时间】:2012-05-03 01:47:56
【问题描述】:

我一直在努力解决 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");
        }
    };
}

【问题讨论】:

    标签: android radio-button radio-group


    【解决方案1】:

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

    请致电b.setChecked(true);


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

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

    奇怪的怪癖。

    【讨论】:

    • 先生:需要您的帮助。请看一下stackoverflow.com/questions/22602559/…
    • 真的救了我的命...精湛的@Sam
    • 请详细回答。我没有得到你的答案。
    • 是的,真的节省了我的时间谢谢@Sam
    【解决方案2】:

    只需在onResume() 方法中发布您的r.setOnCheckedChangeListener(onPromobuttonsClick); 行。它对我有用。

    【讨论】:

      【解决方案3】:

      所以这是一个旧的,但我也有类似的行为。我发现的一种解决方法是调用RadioButtontoggle() 方法而不是RadioGroupcheck() 方法。

      所以在这个例子中,你只需要交换

      r.check(R.id.radio1);
      

      b.toggle();
      

      因为 b 已经是 ID 为 R.id.radio1 的视图。

      【讨论】:

        【解决方案4】:

        当我想记录单选按钮按下的分析时遇到了这个问题,但是使用.check() 处理单选组呼叫的方式导致onCheckedChangeListener 被多次调用(这发送了太多事件)。

        我通过以下方式处理它,每次点击只获得一个事件:

        // create listeners so we don't duplicate the creation in for loop
        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // whatever you want to do here
            }
        };
        
        // add on click listener to all radio buttons
        int buttonCount = buttonGroup.getChildCount();
        for (int i = 0; i < buttonCount; i++) {
            if (buttonGroup.getChildAt(i) instanceof RadioButton) {
                buttonGroup.getChildAt(i).setOnClickListener(onClickListener);
            }
        }
        

        【讨论】:

          【解决方案5】:

          这是一个老问题,但我还没有找到涵盖我的问题的完整答案。但是感谢#Hellojeffy 的建议,我想出了一种方法来处理使用setOnCheckedChangeListener() 时RadioButton 多次触发的情况。 这个想法是在RadioGroup 内的每个视图上添加一个点击,并在点击时触发一个事件,而不是在检查更改时触发一个事件。

          for (child in radioButtonGroup.children) {
                      if (child is RadioButton) {
                          when (child.id) {
                              R.id.rb1 -> child.setOnClickListener { onRb1Click(...) }
                              R.id.rb2 -> child.setOnClickListener { onRb1Click(...) }
                              R.id.rb3 -> child.setOnClickListener { onRb1Click(...) }
                                  ...
                          }
                      }
                  }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-01-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多