Kotlin 的接口回调

class TestView(view: View) {

    val view = view

    private lateinit var listener: (String) -> Unit

    init {
        view.onClick {
            if (::listener.isInitialized) {
                listener("test")
            }
        }
    }

    fun setTestListener(listener: (String) -> Unit) {
        [email protected] = listener
    }

}

调用

var myView = TestView()
myView.setTestListener {
    Logcat.d(it)
}

你可能感兴趣的:(Kotlin 的接口回调)