How do I set the custom interface like OnClickListener


#1

Hey,I try to use Kotlin for my application。Then have a question.
We can set View’s OnClickListener like this
view.setOnClickListener{println(“clicked”}
I have a custom interface
public interface Itest {
public fun runMyCallback(content:String?)
}
in a custom class.
So I want to the custom interface to clas like view.setOnClickListener{println(“clicked”}
How to do it.
Thinks.


#2

Here’s an exerpt from my own Android app that uses listeners:

val myListener = View.OnClickListener {
    ...
    surfaceView.invalidate();
}

liteButton.setOnClickListener(myListener)
toolButton.setOnClickListener(myListener)
numbersButton.setOnClickListener(myListener)
...

If you have a custom interface that extends View.OnClickListener, you could use that here instead and it should also work just fine.


#3

And while you’re converting things:

liteButton.onClickListener = myListener
toolButton.onClickListener = myListener
numbersButton.onClickListener = myListener

Note that IDEA should have placed warnings on these lines of your original code.


#4

Those “buttons” come from the kotlin-android-extensions’s synthetic properties. I just tried the substitution you suggest and it unfortunately doesn’t work. Not defined. Maybe that was in an earlier version?


#5

It’s Not working??