1. 程式人生 > >RadioGroup呼叫check(id)方法時,OnCheckedChangedListener被多次回撥問題

RadioGroup呼叫check(id)方法時,OnCheckedChangedListener被多次回撥問題

在程式碼中選中RadioGroup其中某一個RadioButton選項,有兩種方式:
1. RadioGroup.check(radioButtonId)方法;
2. 直接控制該RadioButton,使用RadioButtton.setChecked(true)方法.
近日偶然發現,使用方法1選中時,RadioGroup設定的OnCheckedChangeListener會被多次回撥(onCheckedChanged方法)。經多次實驗,有如下規律:
假如當前被選中的RadioButton是oneRbtn, 然後通過RadioButtton.setChecked(true)方法選中twoRbtn。此時onCheckedChanged(RadioGroup group, int checkedId)會被回撥三次,第一次傳回的checkedId為oneRbtn,後兩次均為twoRbtn。
此問題暫時不清楚原因和解決方法。只能使用方法2直接控制RadioButton。