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

xml代码

activity_main代码

<?xml version="1.0" encoding="utf-8"?>
<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)

<?xml version="1.0" encoding="utf-8"?>
<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间数据传输)

<?xml version="1.0" encoding="utf-8"?>
<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:
在这里插入图片描述
我将这里的name填写为android.media.action.IMAGE_CAPTURE
category填写为DEFAUTL,即默认。
运行程序,分别点击"打开系统相机"和"显示intent"在这里插入图片描述
可以看到,这个时候比之前又多了一个选择->我们刚刚编写的scecond。(这三个分别是刚刚编写的scecond,系统自带相机,谷歌相机)。用户可以自己选择要打开的app。也就是说,使用隐式Intent也可以启动其他程序的Acticity

在隐式Intent中只能指定一个action,但是可以指定多个category,我们再来修改AndroidManifest.xml:
在这里插入图片描述
再来修改MainActivity.kt:
在这里插入图片描述
运行程序,分别点击"打开系统相机"和"显示intent"
点击"打开系统相机":
在这里插入图片描述
点击"显示intent",直接跳转Scecond

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值