Detailed explanation of Android application developed by kotlin

Detailed explanation of Android application developed by kotlin

Related articles: basic introduction to kotlin language:

http://www.jb51.net/article/114086.htm

We simply know the advantages of kotlin, a new language, and have been exposed to some common grammars and their simple use. I believe you will be very interested in it. Let's understand it as being interested in it for the time being. Hahaha. So how do we apply this new language in Android? Today's article takes you to learn how to use kotlin to develop Android applications, and compares our traditional language Java, so that you can really feel its beauty and elegance.

to configure

Project gradle file

App gradle file:

Anko

Through the above configuration, you will find that anko dependency is introduced. Anko is a powerful library developed by JetBrains. Speaking of JetBrains, it's awesome. They developed the kotlin language, developed the most popular development tool IntelliJ idea, and as is also based on idea. Well, to get back to business, anko is a kotlin library officially developed by kotlin, which makes the development of Android applications faster and simpler, and makes the code we write simpler, clearer and easier to read. It consists of several parts, as follows

Next, let's understand the advantages of kotlin language in developing Android through code.

Never use findviewbyid again

Anyone who has done Android development knows that there are too many layout files written, and findviewbyid is also a great workload. In addition, variables must be declared first, and then forcibly converted into our controls in findviewbyid. The usage is generally as follows

Sometimes it's written to make you feel sick. Some people may say that there is no library with some annotations, such as butterknife. When we use annotations, we can not use findviewbyid. The usage is as follows

Indeed, the use of annotations does reduce our workload, but it is still not the simplest. The simplest thing is that we can directly assign values to the control with ID user. Perhaps you will find it a little incredible. But kotlin did. We can write this directly

See this, do you have a feeling that you hate to meet late? It's too concise. User is the ID declared in our layout file. Text is like settext(). In kotlin language, we can't see the set / get method in Java. It should be noted that when we want to use it like this (instead of using findviewbyid, we need to add the application plugin: 'kotlin Android extensions' in gradle), we need to add the following code

Anko Layout

We usually use XML files to write our layout, but it has some disadvantages, such as not type safe, not empty safe, parsing XML files consumes more CPU and power, and so on. Anko layout can use DSL to dynamically create our UI, and it is much more convenient than using java to dynamically create layout, mainly more concise. It has a hierarchical relationship with XML to create layout, which makes it easier for us to read.

We can remove setcontentview from oncreate method, and then add the above code to display the effect as shown in the following figure, that is, a textview, an EditText and a button are placed in a vertical linear layout. And there is a click event in the button. When clicked, the content of EditText will be displayed as toast.

The above code is not very easy to understand. Of course, the default control can not meet our needs. For example, we will change the font color and size, set the width and height, and set the margin and padding values. How to implement it is also very simple, because its logic and XML writing layout are a routine. For example, the following implementation

I don't think I need to explain the above code, you should see the effect of the control. Because its attribute corresponds to the name of the attribute we set in XML.

In the process of creating the UI above, we directly write the code for creating the UI in the oncreate method. Of course, there is another way to write it. We create an internal class, implement the ankocomponent interface, and override the createview method, which returns a view, that is, the layout we create. Amend as follows

Then add a sentence of code in the oncreate method to create our layout page. as follows

Now we compile and run, and find that the effect is the same as the interface written in the layout file. However, its performance has advantages. In fact, it has not found any performance advantages. In any case, this DSL is really easy to read and get started. In the above code, you may notice dip (10), which means converting 10dp into pixels. It means anko's extension function. If you read anko's source code, we find that a large number of extension functions are used, which is also one of the advantages of kotlin language. It's really powerful, such as dip extension (extract view extension)

Resources.displaymetrics.density and our Java getresources(). Getdisplaymetrics(). Density are the same effect, but I don't think you feel much more comfortable than Java writing.

In the above, we added a click event to the button, and we found that it supports lambda expressions. We want to display a toast. We only need toast ("content"). Is it very concise. In fact, it is also an extension function to implement

Of course, creating a dialog is still very simple, as follows

The more you see, the more comfortable you feel. Ha ha. Another powerful and simple code implementation.

This code implements the effect of asynctask, but you should find that it is much simpler than the Java implementation. Of course, unless it is color blind, you will see the simplicity.

If you use kotlin to develop Android for a period of time, you will find that it reduces the amount of code for us. Of course, we need to explore more advantages and usages ourselves. I believe it will surprise you after exploration.

Implement a simple login interface

The interface is very simple, pseudo code

If it is not complicated, the XML implementation code will not be posted here. If you want to see the XML implementation, you can click to check. Next, only anko implements this layout in the kotlin code.

See how the above code looks. It looks good. Even if you can't write it now, you can read it. Above, we set an event to open mainactivity for the login button. The < > of startactivity describes the activity we want to jump to. If you pass parameters to the open interface, write them directly in (). For example, if we transfer the entered account and password to the jump interface, it will be implemented as

In fact, anko's strength is far more than that. It is worth tasting carefully.

Thank you for reading, hope to help you, thank you for your support to this site!

Source code: http://xiazai.jb51.net/201705/yuanma/FragmentTabhost (jb51.net).rar

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
分享
二维码
< <上一篇
下一篇>>