Android studio之使用Intent在活动中穿梭

本文介绍了如何在Android Studio中使用Intent进行活动间的跳转,包括显式Intent和隐式Intent的使用方法。显式Intent通过指定目标活动类名实现跳转,而隐式Intent通过定义action和category,让系统寻找合适活动来响应。文中详细展示了配置AndroidManifest.xml文件、修改Java代码以及如何实现网页跳转和多category的例子。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

显式Intent

定义两张界面,使用Intent从第一张跳转到第二张
第一个问题,怎么新建第二个界面
第二个问题,怎么让第一个界面视为主界面
第三个问题,如何跳转

一、
在这里插入图片描述
二、
打开下图文件:
在这里插入图片描述
其中红色圈出来为Android studio自动生成的文件,蓝色也是随着Activity生成自动生成,关键就是黑色圈里面的代码就是定义第一张界面首先打印,也是自动生成。

三、
显性Intent实现界面穿梭(关键代码

Intent intent=new Intent(MainActivity.this,Main2Activity.class);
                startActivity(intent);

上面基本讲解完毕。现在设定从***第一个界面点击按钮跳转到第二张界面***

第一个界面布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
 <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="第一页"
     android:layout_gravity="center_horizontal"
     android:textSize="30dp"/>
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="跳转"/>

</LinearLayout>

第一个界面java代码:

package com.example.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button=(Button)findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,Main2Activity.class);
                startActivity(intent);
            }
        });
    }
}

第二个界面布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第二页"
        android:layout_gravity="center_horizontal"
        android:textSize="30dp"/>

</LinearLayout>

效果:在这里插入图片描述
在这里插入图片描述

隐式Intent

相比于显式 Intent,隐式 Intent则含蓄了许多,它并不明确指出我们想要启动哪一个活动,而是指定了一系列更为抽象的 action和 category等信息,然后交由系统去分析这个 Intent,并帮我们找出合适的活动去启动。

什么叫作合适的活动呢?简单来说就是可以响应我们这个隐式 Intent的活动,那么目前Second Activity可以响应什么样的隐式 ntent呢?额,现在好像还什么都响应不了,不过很快就会有了。

通过在< activity>标签下配置< intent- filter>的内容,可以指定当前活动能够响应的action和category

打开AndroidManifest.xml
添加代码:
在这里插入图片描述
同时修改第一个界面的java代码
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
替换为
Intent intent=new Intent(“com.example.activity.ACTION_START”);
注意对应:!!!(上面AndroidManifest.xml里面代码一样需要对应)
在这里插入图片描述
到此隐式Intent就可以跑通了,和显式一样!!
原理:<action标签我们指明了当前活动可以相应com.example.activity.ACTION_START这个action,而<category标签包含一些附加信息,更精确的指明活动还能干啥。在第一个界面java代码里更改新代码
在这里插入图片描述
很明显我们使用了Intent的另一个构造函数,直接将action的字符串传了进去,表明我们想调用的活动是com.example.activity.ACTION_START。但是,我们知道《action》和《category》两个标签同时被响应才能成功启动,为什么此处没有对category的指定呢?因为
<category android:name="android.intent.category.DEFAULT"/>是一种默认的,在调用startActivity时候会自动加进去

到此,也能从第一个界面跳转到第二个界面。

稍微进阶
每个Intent只能指定一个action,但可以指定多个category,所以可以新增加很多,在此增加一个例子:
java文件代码增加一句

intent.addCategory("com.example.activity.MY_CATEGORY");`

同时AndroidManifest.xml文件里面加一句:

<category android:name="com.example.activity.MY_CATEGORY"/>

更多隐式Intent的用法

网页跳转:
将第一个页面的activity中按钮点击事件的代码更改为

public void onClick(View v) {
                //Intent intent=new Intent("com.example.activity.ACTION_START");
                Intent intent=new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("http://www.baidu.com"));
                startActivity(intent);
            }

可以实现调转网址功能
这些代码可有可无!因为直接跳到连接网页不去第二页
在这里插入图片描述
下面介绍一种不太好的方法:(了解即可,因为容易误导用户)
在以上基础上,在建立一个空白Activity
布局文件代码为:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第三页"
        android:layout_gravity="center_horizontal"
        android:textSize="30dp"/>
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="按钮"/>

</LinearLayout>

新Activity的java文件代码不动,在AndroidManifest.xml文件里面添加代码
位置在第三个活动里面

<intent-filter tools:ignore="AppLinkUrlError">
                <action android:name="android.intent.action.VIEW"></action>
                <category android:name="android.intent.category.DEFAULT"></category>
                <data android:scheme="http"></data>
</intent-filter>

运行程序效果为:
在这里插入图片描述
点击跳转:
在这里插入图片描述
如果选择浏览器就是进入网页,如果点击Activity就是进入刚刚写的第三个活动页面(最好先选择仅此一次)
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值