我正在尝试为 setOnFocusChangeListener
构建一个内联函数
这是我到目前为止得到的:
inline fun EditText.onFocusChange(crossinline hasFocus: (Boolean) -> Unit) {
setOnFocusChangeListener(View.OnFocusChangeListener { view, b -> })
}
我是这样用的
freightTimeOfDay.onFocusChange { doSomething() }
不幸的是,虽然它没有给我任何错误,但从未调用过 doSomething()
。
我在这里寻找两件事:
1 - 在其中获取一个参数,以便我可以将其传递给 doSomething()。 例如
freightTimeOfDay.onFocusChange { doSomething(hasFocus) }
2 - 让它工作 :p,因为现在什么都没有发生。
更新:
似乎 kotlin 已经为此提供了某种类型的内联
editText.setOnFocusChangeListener { view, b -> doSomething(b) }
然而,这对我也不起作用,doSomething(hasFocus: Boolean)
永远不会被调用。
提前致谢!
最佳答案
澄清一下,创建内联方法扩展实际上没有意义。 这是我最初的目标,但后来我意识到使用:
editText.setOnFocusChangeListener { view, b -> doSomething(b) }
是可能的,它是内联的,它很漂亮,不需要额外的工作
关于android - 内联 onFocusChange kotlin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51573660/