Android开发教程Kotlin

2.Activity之间的跳转


MainActivity
package com.example.kotlinstudy  import android.content.Intent import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.TextView  class MainActivity : AppCompatActivity() { var tv_first = findViewById<TextView>(R.id.tv_first) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) tv_first.setOnClickListener{ val intent = Intent(this,SecondActivity::class.java) intent.putExtra("name","测试传参") startActivity(intent) } } }
<?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_first" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click Me!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" />  </androidx.constraintlayout.widget.ConstraintLayout>
SecondActivity
package com.example.kotlinstudy  import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.TextView  class SecondActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_second) val name = intent.extras?.getString("name") var tv_content = findViewById<TextView>(R.id.tv_content) tv_content.text = name } }
<?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=".SecondActivity">  <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Second Activity" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" />  </androidx.constraintlayout.widget.ConstraintLayout>
3.Activity之间传参

 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号