【发布时间】:2017-05-05 18:27:36
【问题描述】:
我开始使用 kotlin 在 Android 中进行开发,但我遇到了 lambdas 的问题。我有一个在我的视图中设置监听器的函数,它看起来像这样:
fun setListener(listener: () -> Unit) {
}
The problem is that the code passed as lambda won't be executed in setListener function, it will be executed in another part of my code (specifically when an item of a spinner is selected) so I have to "save" or将此lambda“存储”到变量/属性中,以便我能够在需要时执行它。知道怎么做吗?
编辑:我做到了:
private var listener: (() -> Unit)? = null
fun setListener(listener: () -> Unit) {
this.listener = listener
}
有没有更好的方法呢?谢谢
【问题讨论】: