宁教我负天下人,休教天下人负我!——曹操
AndroidManifest.xml注册
...... <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> ......
创建布局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:layout_width="wrap_content" android:layout_height="wrap_content" android:text="MainActivity" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
AndroidManifest.xml注册
<activity android:name=".TwoActivity" android:parentActivityName=".MainActivity"> <!-- The meta-data tag is required if you support API level 15 and lower --> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity" /> </activity>
创建activity_two.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:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TwoActivity" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
创建Intent-与TwoActivity建立关联
/** Called when the user taps the Send button */ fun sendMessage(view: View) { val textView = findViewById<TextView>(R.id.textView) val message = textView.text.toString() val intent = Intent(this, TwoActivity::class.java).apply { putExtra(EXTRA_MESSAGE, message) } startActivity(intent) }
修改布局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/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="MainActivity" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="sendMessage" android:text="senMessage" tools:layout_editor_absoluteX="148dp" tools:layout_editor_absoluteY="444dp" tools:ignore="MissingConstraints" /> </androidx.constraintlayout.widget.ConstraintLayout>
接受MainActivity传递的消息
class TwoActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_two) // Get the Intent that started this activity and extract the string val message = intent.getStringExtra(EXTRA_MESSAGE) // Capture the layout's TextView and set the string as its text val textView = findViewById<TextView>(R.id.textView).apply { text = message } } }
范型 Class<T>
fun <T : Activity> comToActivity(t: Class<T>,message : String) { val intent=Intent(this, t).apply { putExtra(EXTRA_MESSAGE, message) }; startActivity(intent) }
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } /** Called when the user taps the Send button */ fun sendMessage(view: View) { val textView = findViewById<TextView>(R.id.textView) val message = textView.text.toString() comToActivity(TwoActivity::class.java,message); } fun <T : Activity> comToActivity(t: Class<T>,message : String) { val intent=Intent(this, t).apply { putExtra(EXTRA_MESSAGE, message) }; startActivity(intent) } }
正确使用 android:parentActivityName
<activity android:name=".TwoActivity" android:parentActivityName=".MainActivity"> <!-- The meta-data tag is required if you support API level 15 and lower --> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity" /> </activity> <activity android:name=".ThreeActivity" android:parentActivityName=".MainActivity"> <!-- The meta-data tag is required if you support API level 15 and lower --> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity" /> </activity>
<activity android:name=".TwoActivity" android:parentActivityName=".MainActivity"> <!-- The meta-data tag is required if you support API level 15 and lower --> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity" /> </activity> <activity android:name=".ThreeActivity" android:parentActivityName=".TwoActivity"> <!-- The meta-data tag is required if you support API level 15 and lower --> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".TwoActivity" /> </activity>
取消导航栏默认返回图标
<activity android:name=".TwoActivity"> </activity> <activity android:name=".ThreeActivity"> </activity>
下载资源
参考:
Kotlin范型:https://www.kotlincn.net/docs/reference/generics.html
Kotlin 中的泛型介绍:https://zhuanlan.zhihu.com/p/78721880
【码上开学】Kotlin 的泛型:https://www.bilibili.com/video/av66340216/
Kotlin学习笔记:泛型:https://www.jianshu.com/p/ea7e8356a23b