首页 未分类

实验

首先创建三个Activity,分别叫做MainActivity.kt,MainActivity2.kt,MainActivity3.kt和一个User类。
通过在MainActivity.kt中将参数传到MainActivity2.kt,然后在MainActivity2.kt中又将参数传至MainActivity3.kt
通过

代码

直接上代码
MainActivity.kt:


package cn.iobin.test2

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        main1.setOnClickListener{//给id为main1的TextView设置点击事件
            Intent(this,Main2Activity::class.java).apply {
                putExtra("data","2020-02-22")//传送数据带intent
                putExtra("user",User())//传送User类到intent
                startActivity(this)//
        }

        }
    }
}

MainActivity2.kt:

package cn.iobin.test2

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main2.*

class Main2Activity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)
        main2.setOnClickListener {给id为main2的TextView设置点击事件
            Intent(this,Main3Activity::class.java).apply {
                putExtras(intent.extras!!)//加!!表示不会为空
                startActivity(this)
            }
        }
    }
}

MainActivity3.kt:

package cn.iobin.test2

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main3.*

class Main3Activity : AppCompatActivity() {

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

        var date = intent.getStringExtra("data")
        val user = intent.getParcelableExtra("user") as User
        textView.text = date
        textView4.text = user.name
        textView5.text = user.age.toString()

    }
}

布局文件

activity_main.xml:

<?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/main1"
        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>

activity_main2.xml:

<?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=".Main2Activity">


    <TextView
        android:id="@+id/main2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="155dp"
        android:layout_marginTop="166dp"
        android:text="this is main2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

activity_main3.xml:

<?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=".Main3Activity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="114dp"
        android:layout_height="0dp"
        android:layout_marginStart="52dp"
        android:layout_marginTop="36dp"
        android:layout_marginBottom="36dp"
        android:text="this main3"
        app:layout_constraintBottom_toTopOf="@+id/textView4"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="114dp"
        android:layout_height="0dp"
        android:layout_marginStart="1dp"
        android:layout_marginBottom="52dp"
        android:text="TextView"
        app:layout_constraintBottom_toTopOf="@+id/textView5"
        app:layout_constraintStart_toStartOf="@+id/textView"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="495dp"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/textView4"
        app:layout_constraintStart_toStartOf="@+id/textView"
        app:layout_constraintTop_toBottomOf="@+id/textView4" />

</androidx.constraintlayout.widget.ConstraintLayout>

参考资料

startActivity()详解




文章评论

目录