Kotlin is integrated with the Android SDK (KAD 05) and kotlinandroid

Source: Internet
Author: User

Kotlin is integrated with the Android SDK (KAD 05) and kotlinandroid

By Antonio Leiva

Time: Dec 19,201 6

Link: https://antonioleiva.com/kotlin-integrations-android-sdk/

 

Using the Kotlin language not only simplifies your code, but also simplifies calling Java code from Kotlin.

 

How does this work? Using some common structures, you can get the Kotlin feeling.

 

In this article, you will see some examples of how it improves the same code. It also prevents Java code from including the template part.

 

Integration of Kotlin and Android SDK

For example, the Android framework is based on the Java library. All the Java applications we mentioned earlier are applicable to Android.

 

Let's look at some examples.

 

Getter and setter Methods mapped to attributes

In the previous articles, we have seen getter and setter. This is just an explanation.

 

As we discussed, Kotlin uses attributes instead of fields + getter + setter. The assignment method is the same as get in Java public domain.

 

However, we also know that simple assignment can execute code or implement custom operations.

 

These rules map any setX and getX found in Java to Kotlin X.

 

For example, if you are using TextView, you can use statements similar to attributes to set text:

1 val textView: TextView = ...2 textView.text = "My Text"

 

Of course, this can be applied to any class. You can access (for example) applicationContext or layoutInflater in the Activity in the same way:

1 layoutInflater.inflate(R.layout.view_item, parent)2 val hello = applicationContext.getString(R.string.hello)

 

This does not have to worry about performance. They are actually mapped to the original getter and setter methods, so they are the same as calling them directly.

 

Interface Method ing to Lambda expressions

 

This is surprising. Isn't it necessary to create an anonymous class for anything simple about Android? When using Kotlin, you do not need to do this for the Android listener.

 

The condition is as follows: an interface is required for a method. A typical example is the setOnClickListener of the View class.

 

You can make the following simple statement:

1 view.setOnClickListener { navigateToNextActivity() }

 

How easy to see?

 

If you need to return a view from the original interface, you can overwrite it:

1 view.setOnClickListener { v -> navigateWithView(v) }

 

Note: What is the strange structure? It is a Lambda expression, which indicates that the input parameter of the function is the method defined on the left of the arrow. The right side of the arrow is the function body, and the last line is the result. We will discuss this in subsequent articles.

 

Conclusion

In the Kotlin project, it is easy to integrate other Java libraries, especially the Android framework.

 

To learn more about Kolin, please join this list so that you can receive these articles in your inbox.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.