"The First Line of Code" Notes P15-P45

It's a pity to study this book again. I forgot to take notes after reading it before, which made it feel like I didn't read it.

The content of these 30 pages mainly talks about how to create an Android project, master the use of log tools, an introduction to the Kotlin language, how to run kotlin code in Android Studio, variables, and program logic control.

1. Create an Android program

Click file->new->new project and select this Empty Activity to create it directly

 The Android Studio I installed here is 2022.1.1

But my Android Gradle Plugin Version and Grade Version are 7.4.2 and 7.5

 

 So the created project will report an error

reason:

The following is the reproduced content:

The Android Studio build system is based on Gradle, and the Android Gradle plugin adds several features specific to building Android apps. While the Android plugin is generally updated in tandem with Android Studio, the plugin (and the rest of the Gradle system) can run independently of Android Studio and be updated separately.


I changed the version of the sdk, and then re-synced it to use it.

 

 Second, how to look at an Android project

First look at the AndroidManifest.xml file under the main folder under src (this is the configuration file of the entire Android project, the four major components defined in the program need to be registered in this file, and you can also add permissions to the application in this file statement)

Line 15 marks the main Activity running in the Android project, hold down the ctrl key, and click with the left mouse button. MainActivity will jump to

MainActivity

MainActivity, click the button to locate the file currently selected to open, here you can find that the main Activity class inherits
AppCompatActivity (AppCompatActivity is a subclass of Activity), which is a downward compatible Activity provided by AndroidX, can make the functions of Activity consistent in different system versions. The design of the Android program pays attention to the separation of logic and view . , so it is not recommended to write the page directly in this class, but edit the page in the layout file, and then introduce it in this class. The setContentView method is used in line 9 of the program, so we hold down Ctrl and the left mouse button to jump to this layout file

 

 The TextView that defines Hello World is found in this layout file, and the Hello World in the first program is defined here. 

 Then I talked about the meaning of file resources under res

 The folder beginning with drawable puts pictures

The folder at the beginning of mipmap puts the application icon

The folder at the beginning of values ​​puts strings, styles, colors

The folder at the beginning of layout puts the layout file

Then a new application name string is defined in the strings.xml file under the values ​​folder, and then referenced in the AndroidManifest.xml file, emphasizing

- The reference of the string can be obtained through R.string.app_name in the code

- The reference to the string can be obtained through @string/app_name in xml

 

 3. Android's log tool Logcat

After the Android Studio upgrade, this development tool is gone, so this piece is skipped

4. Introduction to Kotlin language

Kotlin was developed and designed by JetBrains. The first version was announced as early as 2011, and it was open-sourced in 2012, and the official version 1.0 was released in 2013.

Then learn variables, functions, and program logic control (if conditional statement, when conditional statement)

To build kotlin in android studio, it needs to be built under the package of the same level as MainActivity

 

 

Not much nonsense, just go to the code

package com.czy.helloworld

fun main() {
    println("Hello World")

    var a = 10
    a *= 10
    println("a=" + a)

    //使用两个数相加函数
    val result = addTowNum(1, 11)
    println("1+11=$result")

    //两个数相减
    val result2 = twoNumSub(10, 1)
    println("10-1="+result2)

    //两个数比大小
    val largerNumber1 = largerNumber1(10, 2)
    println("两个数比大小 第一种写法 java风格 10 与 2 最大的是 $largerNumber1")
    val largerNumber2 = largerNumber2(55, 1)
    println("两个数比大小 第二种写法 55 与 1 最大的是 $largerNumber2")
    val largerNumber3 = largerNumber3(7, 1)
    println("仔细发现value其实也是一个多余的变量,可以直接返回,这样能使代码更加简洁 7 与 1 最大的是 $largerNumber3")
    val largerNumber4 = largerNumber4(9, 99)
    println("两个数比大小 第三种写法 9 与 99 最大的是 $largerNumber4")
    val largerNumber5 = largerNumber5(998, 9999)
    println("第三种方法还可以直接压缩为一行代码 998 与 9999 最大的是 $largerNumber5")

}


//两个数相加
fun addTowNum(param1: Int, param2: Int): Int {
    return (param1 + param2)
}

//两个数相减 first - second
fun twoNumSub(param1: Int,param2: Int) = (param1 - param2)


//两个数比大小 第一种写法 java风格
fun largerNumber1(num1:Int,num2:Int):Int{
    var value = 0
    if (num1>num2){
        value = num1
    }else{
        value = num2
    }
    return value
}

/*
    两个数比大小 第二种写法
    Kotlin中的if相比于java有一个额外的功能,它是可以有返回值的,
    返回值就是if语句每一个条件中最后一行代码的返回值
*/
fun largerNumber2(num1 :Int,num2:Int):Int{
    val value = if (num1>num2){
        num1
    }else{
        num2
    }
    return value
}

/*
    两个数比大小 第二种写法
    仔细发现value其实也是一个多余的变量,可以直接返回,这样能使代码更加简洁
*/
fun largerNumber3(num1 :Int,num2:Int):Int{
    return if (num1>num2){
        num1
    }else{
        num2
    }
}


/*
    两个数比大小 第三种写法
    当一个函数只有一行代码时,可以省略函数体部分,直接将这一行代码使用等号串联在
    函数定义的尾部
*/
fun largerNumber4(num1 :Int,num2:Int) = if (num1>num2){
        num1
    }else{
        num2
    }

/*
    两个数比大小 第三种写法
    第三种方法还可以直接压缩为一行代码
*/
fun largerNumber5(num1 :Int,num2:Int) = if (num1>num2) num1 else num2












package com.czy.helloworld

fun main() {
    val score = getScore1("Tom")
    println(score)
    val score2 = getScore2("Jim")
    println(score2)
}


/**
 * 根据名字比较分数
 * if版本
 */
fun getScore1(name:String) = if (name =="Tom") 86 else if(name =="Jimi") 56 else if (name == "Lilei") 75 else 0


/**
 * 根据名字比较分数
 * when版本  匹配值 -> {执行逻辑}
 */
fun getScore2(name:String) = when(name){
    "Tom" -> 86
    "Jim" ->100
    "Lilei" -> 45
    else -> 0
}

Thoughts on kotlin code:

   1. It is fully compatible with java-style codes. If you don’t want the kotlin language, you can run it directly with java.

   2. The language style of kotlin is very simple and flexible. It has auto deduced object data type

  3. val declares a constant,  and the reference is immutable; var declares a variable, which can be reassigned, and the reference can be changed.

Comparison between java and kotlin data types

Java basic data types kotlin object data type Data Type Description
int Int plastic surgery
long Long long integer
short Short short integer
float Float single precision floating point
double Double double precision floating point
boolean Boolean Boolean
char Char string type
byte Byte Byte

 

To quote the blog:

(4 messages) Correspondence between Java types and Kotlin types_Kotlin and jdk relationship_WongKyunban's Blog-CSDN Blog

Guess you like

Origin blog.csdn.net/weixin_46511995/article/details/129846017