我有一些MaterialButton
改变检查状态的 s(在切换组场景中)。
我的计划是通过MaterialButton
s监听 Checked 状态addOnCheckedChangeListener
但是当某些情况发生时我需要删除侦听器,如何从 lambda 获取侦听器(对象)(并将其放入onCheckedChangeListener
值中)?
var onCheckedChangeListener: MaterialButton.OnCheckedChangeListener?=null
...
myMaterialButton?.addOnCheckedChangeListener { button, isChecked ->
Log.i(TAG, "onViewCreated: button = $button, isChecked = $isChecked")
if(button.isChecked)
{
//Use some variables which are reachable here
//Do som work
}
else
{
//Use some variables which are reachable here
//Do some other work
}
}
...
//Some condition prior to this causes me to remove the listener by using
//removeOnCheckedChangeListener, but how do I get the onCheckedChangeListener from my
//lambda above which I need to remove ?
myMaterialButton?.removeOnCheckedChangeListener(onCheckedChangeListener)
解决方案当然很简单,但我找不到任何明显的答案。