In Android Studio to use Kotlin

In Android Studio to use Kotlin

First of all to explain why to learn Kotlin, because the future may be closed-source Java, Android team announced Kotlin language as the official first-class support in Google I / O 2017 conference. But after the first version of Android Studio languages are also Kotlin rather than the Java , another point also own experience, which is now out of the interview Andorid development, the interviewer will ask you know Kotlin? How long can grasp? . This is my own experience, it is important you do not have to say, similar to the system as Huawei obscurity, Kotlin will be as positive as the spare tire, the following entry to the topic.
1. Create a Kotlin language in Android Studio above the main items
shown below to create a project
Here Insert Picture Description
After you finish creating open MainActivity, you can see the different changes, the code shown below

package com.example.kotlintest

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

Before using Java-based language with MainActivity there are some differences, but also to see the familiar is

		super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

It will not be introduced, (Another to note is that in Kotlin inside end of the sentence can not add a semicolon, this is not enough in Java) Now we open activity_main.xml file, put inside a TextView and given an ID
code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_tip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Then back to MainActivity , this time by the Java logic, then we must first instantiate the control, give control assignment, and page rendering, I believe that all know, the use Kotlin , then how to instantiate the controls it?

First import a package

import kotlinx.android.synthetic.main.activity_main.*

Then copy and paste into your control id onCreate the following method
if you do not like to manually copy the package into the way id can copy the control to the onCreate the following methods, and then right-click on the id press Alt + Enter to import import, it will automatically add the above which package, then we assign to this text, I have here the default value is the Hello World , (PS: No matter what learning new languages, Hello World, are to be the first attempt,), I have to change its value, the
code is as follows:

tv_tip.text = "Hello Money!"

This syntax with Java on the very different.
We look at the text of the method
Here Insert Picture Description
and then we run it, view it on the phone

Here Insert Picture Description
What a plain page, and I do not talk about the idea that love story, I just want to Gaoqian.
OK, we first come here, product demand has also changed, and I have to go to achieve the function.

Published 25 original articles · won praise 6 · views 9176

Guess you like

Origin blog.csdn.net/qq_38436214/article/details/102839786