How to customize view with kotlin for Android

preface

With Google's announcement of kotlin as the official development language, the trend of using kotlin in Android is becoming more and more obvious. Recently, it was bombed by kotlin's articles, so I decided to try it. After trying, I feel it's magical to rely on it. Especially when writing a complex cycle in one sentence, I was stunned. Moreover, using as, java code can be directly converted to kotlin.

The renderings are as follows:

The first is the rendering of the customized view, which is a pie chart. If it is written in Java, it will be dozens of lines. I think it may be less if it is kotlin.

Sample code

The main function is to set the number of data arbitrarily. Here are 4 data, and the color of each data can be set arbitrarily.

#####Start with the kotlin code#####

Set data

#####Java code with the same function#####

Tell me about the differences between kotlin and Java. The first is the life of variables. Kotlin must be assigned or initialized when declaring. Java doesn't need it. I'm not used to it at first. Kotlin doesn't need to end with a semicolon. Kotlin's loop is easy to use.

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>