Android RdioGroup clearCheck 与 setOnCheckedChangeListener 冲突
Android RdioGroup clearCheck conflict with setOnCheckedChangeListener
我正在尝试在单击单选按钮时执行操作:
dataBinding.radioGroup.setOnCheckedChangeListener { radioGroup: RadioGroup, i: Int ->
if( radioGroup.checkedRadioButtonId != -1){
checkAnswer(i)
}
}
在另一个函数中有一个 clearCheck
清除单选按钮。
问题是每次调用 clearCheck
时,监听器 setOnCheckedChangeListener
也会调用。
有什么方法可以让 运行 侦听器在选择单选按钮而不是清除单选按钮时获得 运行?
/**
* <p>Interface definition for a callback to be invoked when the checked
* radio button changed in this group.</p>
*/
public interface OnCheckedChangeListener {
/**
* <p>Called when the checked radio button has changed. When the
* selection is cleared, checkedId is -1.</p>
*
* @param group the group in which the checked radio button has changed
* @param checkedId the unique identifier of the newly checked radio button
*/
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId);
}
当你在 RadioGroup 上调用 clearCheck()
方法时,如果有一个 RadioButton 被选中,它会将这个视图的状态设置为未选中,然后通知应用程序一个单选按钮的状态被改变调用您传递给 setOnCheckedChangeListener()
方法的实例的 onCheckedChanged()
方法。
这解释了为什么在您的应用中,如果用户 select 回答错误,它会调用 onCheckedChange()
两次。前者是状态由unchecked变为checked,后者是状态由checked变为unchecked clearCheck()
。这是预期的行为。
解决方法:调用clearCheck()
方法前清除OnCheckedChangeListener,调用clearCheck()
方法后再设置
// Declare this variable
private val onCheckedChangeListener = object : RadioGroup.OnCheckedChangeListener {
override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
if (checkedId != -1) {
checkAnswer(checkedId)
}
}
}
// Calling this line once, inside onCreate() of Activity
// or onCreateView() of Fragment for example
dataBinding.radioGroup.setOnCheckedChangeListener(onCheckedChangeListener)
private fun checkAnswer(checkedId: Int) {
// Your logic to check answer here
// ...
// If the answer is wrong, just clear check
dataBinding.radioGroup.setOnCheckedChangeListener(null)
dataBinding.radioGroup.clearCheck()
dataBinding.radioGroup.setOnCheckedChangeListener(onCheckedChangeListener)
}
我正在尝试在单击单选按钮时执行操作:
dataBinding.radioGroup.setOnCheckedChangeListener { radioGroup: RadioGroup, i: Int ->
if( radioGroup.checkedRadioButtonId != -1){
checkAnswer(i)
}
}
在另一个函数中有一个 clearCheck
清除单选按钮。
问题是每次调用 clearCheck
时,监听器 setOnCheckedChangeListener
也会调用。
有什么方法可以让 运行 侦听器在选择单选按钮而不是清除单选按钮时获得 运行?
/**
* <p>Interface definition for a callback to be invoked when the checked
* radio button changed in this group.</p>
*/
public interface OnCheckedChangeListener {
/**
* <p>Called when the checked radio button has changed. When the
* selection is cleared, checkedId is -1.</p>
*
* @param group the group in which the checked radio button has changed
* @param checkedId the unique identifier of the newly checked radio button
*/
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId);
}
当你在 RadioGroup 上调用 clearCheck()
方法时,如果有一个 RadioButton 被选中,它会将这个视图的状态设置为未选中,然后通知应用程序一个单选按钮的状态被改变调用您传递给 setOnCheckedChangeListener()
方法的实例的 onCheckedChanged()
方法。
这解释了为什么在您的应用中,如果用户 select 回答错误,它会调用 onCheckedChange()
两次。前者是状态由unchecked变为checked,后者是状态由checked变为unchecked clearCheck()
。这是预期的行为。
解决方法:调用clearCheck()
方法前清除OnCheckedChangeListener,调用clearCheck()
方法后再设置
// Declare this variable
private val onCheckedChangeListener = object : RadioGroup.OnCheckedChangeListener {
override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
if (checkedId != -1) {
checkAnswer(checkedId)
}
}
}
// Calling this line once, inside onCreate() of Activity
// or onCreateView() of Fragment for example
dataBinding.radioGroup.setOnCheckedChangeListener(onCheckedChangeListener)
private fun checkAnswer(checkedId: Int) {
// Your logic to check answer here
// ...
// If the answer is wrong, just clear check
dataBinding.radioGroup.setOnCheckedChangeListener(null)
dataBinding.radioGroup.clearCheck()
dataBinding.radioGroup.setOnCheckedChangeListener(onCheckedChangeListener)
}