Kotlin implements Android programs


Preface

This article mainly introduces the basic usage of Kotlin to realize the Activity in the Android component; it mainly includes: Jump between Activities and transfer values ​​between Activities


One, use Kotlin to create Android programs

1. Choose to create Kotlin language
Insert picture description here
(2) There will be a failure problem during the creation process. The basic reason is that the Android Studio version is small. Follow the prompts to update Android Studio.
(3) Create a Kotlin class, choose to create Kotlin, and choose class at the same time
Insert picture description here
Insert picture description here

(4) Create Activity, the same as java1
Insert picture description here

Two, jump between activities

We create two activities, MainActivity only contains a Button button, SecondActiviy contains a TextView, the layout is too simple and will not be displayed, here only one id is displayed, the id will be useful later

android:id="@+id/button"  //Button的ID
  android:id="@+id/tv_contnet" //TextView的ID

We want to implement the click button to jump to the interface, first let's look at how Java jump

Java code

 btn = findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View view) {
    
    
                Intent intent =new Intent(MainActivity.this,SecondActivity.class);
                startActivity(intent);
            }
        });

In Kotlin, you don’t need to get the control. Instead, you can directly write the same value as the id in the Activity to get and set the event. You can know through the code hint.
Insert picture description here
When creating a click event, select the second
Insert picture description here
Kotlin code:

button.setOnClickListener {
    
    
    Intent(this, SecondActivity::class.java).apply {
    
    
        startActivity(this)
    }
}

It can be seen that Kotlin does simplify the amount of code. If you are not used to the above code or have never used it, it is normal to have multiple codes, just like you wrote Java before. Practice makes perfect

Three, transfer parameters between activities

Common type

Java parameter transfer

在这里插入代码片

Kotlin Passage

Pass parameters

button.setOnClickListener {
    
    
    Intent(this, SecondActivity::class.java).apply {
    
    
        putExtra("name","mt")
         putExtra("age",18)
        startActivity(this)
    }
}

Accept the parameters
here, pass the intent directly, you don’t need to go to Intent intent = getIntent() like Java

       var name = intent.extras?.getString("name")
     tv_contnet.text =name

Serializable for non-common types

Can be achieved by implementing Serializable or Parcelable interface
1.Serializable

    class SeriUser :Serializable {
    
    
    var name:String?="serializeable"
    var age:Int=1000
}

Pass parameters

          Intent(this, SecondActivity::class.java).apply {
    
    
             putExtra("user", SeriUser())
               startActivity(this)
           }

Accept parameters (note that you need to add as SeriUser at the end, otherwise an error will be reported)

 //        非普通类型,且时Serializable
        var serUsers =intent.getSerializableExtra("user") as SeriUser
        tv_contnet.text=serUsers.name
        

Parcelable of non-common type

2.Parcelable


class User() :Parcelable {
    
    
    var name:String?="test name"
    var age:Int =100
    constructor(parcel: Parcel) : this() {
    
    
    //必须实现
        name =parcel.readString()
        age =parcel.readInt()
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
    
    
    //必须实现
        parcel.writeString(name)
        parcel.writeInt(age)

    }

    override fun describeContents(): Int {
    
    
        return 0
    }

    companion object CREATOR : Parcelable.Creator<User> {
    
    
        override fun createFromParcel(parcel: Parcel): User {
    
    
            return User(parcel)
        }

        override fun newArray(size: Int): Array<User?> {
    
    
            return arrayOfNulls(size)
        }
    }
}

Pass parameters

            Intent(this, SecondActivity::class.java).apply {
    
    
                putExtra("user", User())
                startActivity(this)
            }

Accept parameters

//        非普通类型,且时Parcelable
       var user =intent.getParcelableExtra<User>("user") as User
       tv_contnet.text =user.name

Guess you like

Origin blog.csdn.net/qq_45353823/article/details/108905468