kotlin实现使用Intent进行activity之间的跳转

kotlin实现使用Intent进行activity之间的跳转

  • xml代码
    • activity_main代码
    • activity_scecond代码(用于显式intent)
    • activity_hello代码(用于显式intent及activity间数据传输)
  • Kotlin代码
    • MainActivity代码
    • Scecond代码(用于显式intent)
    • Hello代码(用于显式intent及activity间数据传输)
  • 效果图
  • 注意

xml代码

activity_main代码


<RelativeLayout 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"
    android:background="@drawable/bg_environment">

    <LinearLayout
        android:layout_width="1200px"
        android:layout_height="800px"
        android:background="@drawable/bg_frame_descend_setting"
        android:layout_centerInParent="true"
        android:orientation="vertical">

        <Button
            android:id="@+id/camera"
            android:layout_width="600px"
            android:layout_height="wrap_content"
            android:background="@drawable/btn_page_hover"
            android:text="打开系统相机"
            android:layout_gravity="center_horizontal"/>

    LinearLayout>

    <Button
        android:id="@+id/transfer"
        android:layout_width="400px"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@drawable/btn_page_hover"
        android:text="打开欢迎界面"/>

    <Button
        android:id="@+id/button"
        android:layout_width="400px"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:background="@drawable/btn_page_hover"
        android:text="显示intent"/>

RelativeLayout>

activity_scecond代码(用于显式intent)


<RelativeLayout 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=".ScecondActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="测试显式Intent"/>

RelativeLayout>

activity_hello代码(用于显式intent及activity间数据传输)


<LinearLayout 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=".Hello">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="6666666666"/>

LinearLayout>

Kotlin代码

MainActivity代码

package com.example.yyr2_8

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

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        camera.setOnClickListener {
            //使用隐式intent的方法打开相机
            var intent = Intent("android.media.action.IMAGE_CAPTURE")
            startActivity(intent)
        }
        transfer.setOnClickListener {
            val data = "Hello YuYiRan"
            val intent = Intent(this,Hello::class.java)
            intent.putExtra("your_name",data)
            //putExtra接受一个键值对,即(key,value).在其他activity之间调用的时候只需要知道key就可以知道value
            startActivity(intent)
            //注意:这里只是向下一个activity传输数据,向上一个activity传输数据不能用startActivity
        }
        button.setOnClickListener {
            //显式activity
            val intent = Intent(this,ScecondActivity::class.java)
            startActivity(intent)
        }
    }
}

Scecond代码(用于显式intent)

package com.example.yyr2_8

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class ScecondActivity : AppCompatActivity() {

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

Hello代码(用于显式intent及activity间数据传输)

package com.example.yyr2_8

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

class Hello : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_hello)
        val YourName = intent.getStringExtra("your_name")
        //这里只需将MainActivity中的key填写进去,getStringExtra会自动将key的value提取出来
        textView.text = YourName
    }
}

效果图

在这里插入图片描述

注意

对于隐式Intent,我的例程是打开系统自带的相机的文件,我们当然也可以隐式打开自己的activity。比如说我想隐式打开我的Scecond,呢我们需要先修改AndroidManifest.xml:
kotlin实现使用Intent进行activity之间的跳转_第1张图片
我将这里的name填写为android.media.action.IMAGE_CAPTURE
category填写为DEFAUTL,即默认。
运行程序,分别点击"打开系统相机"和"显示intent"kotlin实现使用Intent进行activity之间的跳转_第2张图片
可以看到,这个时候比之前又多了一个选择->我们刚刚编写的scecond。(这三个分别是刚刚编写的scecond,系统自带相机,谷歌相机)。用户可以自己选择要打开的app。也就是说,使用隐式Intent也可以启动其他程序的Acticity

在隐式Intent中只能指定一个action,但是可以指定多个category,我们再来修改AndroidManifest.xml:
kotlin实现使用Intent进行activity之间的跳转_第3张图片
再来修改MainActivity.kt:
kotlin实现使用Intent进行activity之间的跳转_第4张图片
运行程序,分别点击"打开系统相机"和"显示intent"
点击"打开系统相机":
kotlin实现使用Intent进行activity之间的跳转_第5张图片
点击"显示intent",直接跳转Scecond

你可能感兴趣的:(kotlin)