Home > Software design >  How to define a variable which implement the interface in Kotlin?
How to define a variable which implement the interface in Kotlin?

Time:05-06

I hope to define a variable mAction which implement the interface MediaRecorder.OnInfoListener .

But Code A isn't correct, how can I fix it?

Code A

  val mAction: MediaRecorder.OnInfoListener{
        mr, what, extra ->
           if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
               //ToDo
          }
    }

CodePudding user response:

  val mAction = object: MediaRecorder.OnInfoListener{
        mr, what, extra ->
           if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
               //ToDo
          }
    }

you can achieve this by doing something like this

CodePudding user response:

You are simply missing an = instead of :.

val mAction = MediaRecorder.OnInfoListener{
        mr, what, extra ->
           if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
               //ToDo
          }
    }

You can’t define a property without = unless you are defining a custom getter or using a property delegate.

  • Related