Get to know the Swift on the Android platform of Kotlin.

Source: Internet
Author: User

Get to know the Swift on the Android platform of Kotlin.

The Kotlin syntax is concise and has the characteristics of a series of dynamic languages, Lambda expressions, high-order functions, closures, and even type checks for static languages. The core point is to write the same function, kotlin requires a lot less code.

The following is a JAVA data class

public class Artist {    private long id;    private String name;    private String url;    private String mbid;    public long getId() {        return id;    }    public void setId(long id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getUrl() {        return url;    }    public void setUrl(String url) {        this.url = url;    }    public String getMbid() {        return mbid;    }    public void setMbid(String mbid) {        this.mbid = mbid;    }    @Override public String toString() {        return "Artist{" +                "id=" + id +                ", name='" + name + '\'' +                ", url='" + url + '\'' +                ", mbid='" + mbid + '\'' +                '}';    }}

If it is C #3.0 or above, you will not see a bunch of Set () and Get () methods, which are more concise

public Class xxxx{    public int AAAA {set;get;}    public string BBBB {set;get;}    ......}

But the Kotlin code is too concise, better than C #.

data class Artist(    var id: Long,    var name: String,    var url: String,    var mbid: String)

For another example, when developing an Android App using JAVA, you always need to call findViewById to obtain the control in the Activity.

private ShimmerTextView mActionBarTitle; mActionBarTitle = (ShimmerTextView) view.findViewById(R.id.tv_shimmer);

This method is cumbersome and annoying. You must first define a control type and call findViewById to obtain the control object. However, in Kotlin, The findViewById method is completely discarded, for example, if an Activity contains a TextView whose ID is myMessage, You can import it in Kotlin.

import kotlinx.android.synthetic.another_activity.myMessage

Now you can access TextView by using id, just as this TextView is an attribute of the Activity class. For example, modify the string displayed in TextView:

myMessage.setText("Hello")

What if you want to access many controls? Importing data one by one is still very troublesome. In this case, you can choose to import all the controls on the Activity.

import kotlinx.android.synthetic.another_activity.*

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.