美文网首页
Android学习笔记(一):使用Intent实现Activit

Android学习笔记(一):使用Intent实现Activit

作者: CoderAPang | 来源:发表于2019-09-27 10:21 被阅读0次

Intent 是一个消息传递对象,可以用来从Actibity组件之间进行请求传递。
Activity 是Android四大组件之一,是用户交互的入口,表示应用中的一个屏幕。

使用Intent实现Activity页面之间的跳转有两种方式,分别是显示Intent和隐式Intent。
本文将用两种方式分别实现从MainActivity跳转到SecondActivity

一、准备工作:新建MainActivity和SecondActivity,并在MainActivity中添加一个按钮

目录结构

建立两个Activity,分别是MainActivity和SecondActivity,目录如下。在layout目录下自动生成了两个Activity对应的XML文件,在activity_main.xml页面中添加一个Button,命名为bt_send。代码如下:

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">

    <Button
        android:id="@+id/bt_send"
        android:layout_width="93dp"
        android:layout_height="67dp"
        android:layout_marginTop="84dp"
        android:layout_marginEnd="168dp"
        android:layout_marginRight="168dp"
        android:text="send"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

android:id="@+id/bt_send"表示该button的id是bt_send。

activity_second.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=".SecondActivity">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="second Activity"
        tools:layout_editor_absoluteX="42dp"
        tools:layout_editor_absoluteY="49dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

二、显式Intent实现Activity之间的跳转

显式Intent:通过指定Intent组件名称来实现的,它一般用在知道目标组件名称的前提下,一般是在相同的应用程序内部实现的。

mainActivity:

package com.example.myfirstapp;


import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends AppCompatActivity {
    private Button button;//1、定义一个button元素
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button)findViewById(R.id.bt_send);//2、绑定button成员和页面上的bt_send按钮

    //3、button设置onclick监听事件
        button.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent intent =  new Intent(MainActivity.this,SecondActivity.class);//4、设置显式Intent实现从MainActivity到SecondActivity的跳转
                startActivity(intent);//5、调用startActivity()方法,启动SecondActivity
            }
        });
    }
}

主要有五处修改:
1、定义一个button元素
2、绑定button成员和页面上的bt_send按钮
3、button设置onclick监听事件
4、设置显式Intent实现从MainActivity到SecondActivity的跳转
5、调用startActivity()方法,启动SecondActivity
此刻就完成了显式Intent页面跳转,点击MainActivity中的send按钮后,跳转到了SecondActivity:


MainActivity
SecondActivity

我们回过头来在看一下android的配置文件manifests.xml,其中注册了MainActivity和SecondActivity,但是只有MainActivity中设置了Intent-filter,绑定了android.intent.action.MAIN,这是设置APP启动后的第一个入口为MainActivity。

manifests.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myfirstapp">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SecondActivity"></activity>
    </application>

</manifest>

三、隐式Intent实现Activity之间的跳转

隐式Intent:通过Intent Filter来实现的,它一般用在没有明确指出目标组件名称的前提下,一般是用于在不同应用程序之间。隐式Intent实现Activity之间的跳转需要在Manifest.xml中增加Activity的声明,并设置对应的Intent Filter和Action,才能被Android的应用程序框架所匹配。
1、首先在manifests.xml配置文件中给SecondActivity设置<intent-filter>,绑定action,命名为“android.intent.action.MY_ACTION”
2、在MainActivity中修改跳转方式

manifests.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myfirstapp">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SecondActivity">
            <intent-filter>
                <action android:name="com.android.activity.MY_ACTION"/>
                <category android:name = "android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

mainActivity:

package com.example.myfirstapp;


import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends AppCompatActivity {
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button)findViewById(R.id.bt_send);

        button.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setAction("com.android.activity.MY_ACTION");//隐式intent跳转
                startActivity(intent);
            }
        });
    }
}

使用隐式Intent,Android需要解析,将Intent映射给可以处理该Intent的Activity组件等。Intent的解析机制主要是通过查找已经注册在manifests.xml中的所有IntentFilter以及其中定义的Intent,最终找到匹配的Intent。

参考内容:
Android开发学习笔记:浅谈显示Intent和隐式Intent
google官方文档:Intent 和 Intent 过滤器

相关文章

网友评论

      本文标题:Android学习笔记(一):使用Intent实现Activit

      本文链接:https://www.haomeiwen.com/subject/lzhbyctx.html