Method of realizing Android click event with kotlin
Recently, Google announced that kotlin has become a first-class development language for Android. So I just made a simple study. When I checked the data, I found that there were still very few ready-made data, so I decided to record it myself for easy viewing in the future and for reference to others.
In Android, click events can be written in three ways:
1. Anonymous inner class.
2. The activity implements the global onclicklistener interface.
3. Specify the onclick attribute of XML.
Today, kotlin implements these three ways to implement click events
Anonymous inner classes: This is the easiest way
New onclicklistener is not required here.
Globally implement the onclicklistener interface:
The difference between this method and Java is that there is no implementation keyword to represent the implementation interface.
When is equivalent to switch in Java.
Change the ':' symbol to '>'.
Specify the onclick property:
The above is how to write these three click events. If you don't understand the grammar and don't know what the keyword means, you can check the official documents. I hope it will help you in your study, and I also hope you can support programming tips.