First acquaintance with Android Kotlin

One. Introduction to Kotlin

  In 2017, Google announced the support of Kotlin in its Android Studio IDE. Kotlin began to become a first-level language for Android development. At that time, the industry was not very optimistic, because Java has made a huge contribution to the development of Android. The concept of language has been deeply rooted, and it has been constantly improving.
However, in just a few years, Kotlin quickly occupied the market due to its advantages such as more security and simplicity compared to Java. Chet Haase, chief evangelist for Google Android, said: "In the past two years, the popularity of Kotlin has been increasing. More than 50% of professional Android developers now use Kotlin to develop their applications". This can also be seen in the data of this year's Stack Overflow annual developer survey. As shown in the figure below, the report pointed out that Kotlin received 72.6% of the developers' favorite programming languages.

1.1. The brilliance and shadow of Java

  In 1995, when Sun was in full swing, the Java language was released, which caused a huge sensation. Compared with the mainstream C and Basic languages ​​at the time, the Java language was simple, object-oriented, stable, platform-independent, interpreted, multithreaded, Dynamic and other features, like opening a new world, swept the world for a time, gathered a lot of people, Microsoft made C# language in order to imitate Java, Netscape forced a JavaScript language in order to follow the fashion, IBM made Java with its nose IDE Eclipse (Eclipse, ha ha). Until now, Java still occupies a pivotal position in the programming world. When Andy Rubin developed the Android system, he also naturally adopted Java and C++ (C++ is responsible for NDK development) as the development language.
  However, Java is a language more than 20 years ago. Although it has been continuously expanded and updated, the underlying design ideas are difficult to change, which makes it difficult to implement some new language features, such as functional programming, Lambda expressions, Streaming API, high-level functions, null pointer safety, etc. (Although Java 8 implements some features, Android does not support Java 8). These new language features are well received. It can be said that the productivity of programming is liberated. This also shows A fact: development efficiency/time is the real bottleneck of software companies, and any measures that can reduce the amount of code and improve development efficiency should be taken seriously.
  Moreover, Android still has the problem of a Java copyright crisis. Oracle, which acquired Sun, has asked Google for a huge amount of Java copyright fees, which may also speed up Google's search for an alternative language for Android.
  Apple is already replacing the Object-C language with Swift, and Google has also found a language that replaces Java, which is Kotlin promoted by JetBrains (Android Studio is also modified by the company's Intelli J).
  In fact, Swift and Kotlin are quite similar. There is an article Swift is like Kotlin that briefly compares these two languages.

1.2. The emergence of Kotlin

  Kotlin is also a programming language designed based on JVM, which is considered a mild improvement of Java. She is the result of an open source project and has a high reputation. Many companies, organizations, and industry rushers like her. Square’s Jake (Dagger) , ButterKnife, Retrofit, OkHttp...) wrote an article about Using Project Kotlin for Android as the Kotlin platform.
Compared with Java, Kotlin has the following advantages when writing code: simple and efficient code, functional programming, null pointer safety, support for lambda expressions, streaming API, etc.
  In terms of execution efficiency, Kotlin and Java have the same theoretical speed (both are compiled into JVM bytecode).
  In addition, the new language must consider compatibility. In order to coexist harmoniously with the existing project code, Kotlin and Java are perfectly compatible with each other. The two code files can coexist, the code can call each other, the files can be converted, and the library files can be barrier-free. Calling each other, it is said that using Kotlin will not bring additional cost burden.

two. Kotlin layout

  As an improvement of Java, Kotlin has many advantages in Android development. Let's start with the relatively intuitive interface drawing to understand, then look at the grammatical characteristics of Kotlin, and then slowly get in touch with deeper programming ideas.

2.1. Simplify findViewById

  We know that in the Android architecture, the xml layout file and the Activity are loosely coupled. To use interface elements in the Activity, you must use the R file to record the xml control and find this element with findViewById.
In Kotlin, we can continue to use findViewById to bind the controls in the xml layout: (TextView)findViewById(R.id.hello); Kotlin provides a more radical method, completely cancel the findViewById function, directly use the control Id, and other The usage is the same as java.

2.1.1. Layout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
<TextView
        android:id="@+id/tv_main_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:text="Hello World!"
        android:textSize="18sp" />
    <ImageView
        android:id="@+id/iv_main_img"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:scaleType="fitCenter" />
</LinearLayout>

2.1.2. Logic code

class MainActivity : AppCompatActivity() {
    
    
    override fun onCreate(savedInstanceState: Bundle?) {
    
    
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        tv_main_txt.setText("kotlin文档")
        tv_main_txt.setTextColor(
            ContextCompat.getColor(this, R.color.blue)
        )
        iv_main_img.setImageResource(R.drawable.icon_img)
    }
}

2.1.3. Effect picture

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_36158551/article/details/105359610