How I can use callback in Kotlin?

后端 未结 7 2172
Happy的楠姐
Happy的楠姐 2020-12-13 04:05

I have View and one CircleShape , which should show toast in this View. And I use it in main Activity. This is my interface

interface OnClickListenerInterfa         


        
7条回答
  •  再見小時候
    2020-12-13 04:14

    A more simpler solution by using lambda.

    Inside CircleShape.kt, declare a lambda function.

    var listener: (()->Unit)? = null
    ...
    // When you want to invoke the listener
    listener?.invoke()
    

    Inside your Activity

    mCircleShape.listener = {
        // Do something when you observed a call
    }
    

提交回复
热议问题