Android Intent隐式使用

创建工程:

在MainActivity对应的activity_main.xml视图中创建一个button,值是1

在SecondActivity对应的second_layout创建一个button作为跳转后的视图,值是:第二个视图

实现过程解析在代码中:

 

实现效果:

在上图中点击button跳转:

MainActivity代码和解析如下:

package com.example.reactest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
//    向下兼容的Activity 其中AppCompatActivity是Activity的子类 Activity是Android系统提供的一个活动基类

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /*
        * 隐式使用Intent    需要先在    AndroidManifest.xml 中指定当前活动可以响应action和category  category种类,分类
        *   这时进入    AndroidManifest.xml 给要跳转的视图活动SecondActivity添加代码:
        * 将<activity android:name=".SecondActivity"></activity>修改成:
        *   <intent-filter>
                <action android:name="com.example.reactest.ACTION_START"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
         *  注明:路径是根据自己的路径写的
         * 说明:
         * action 这个标签指明当前活动可以响应    com.example.reactest.ACTION_START
         * category 标签包含附加信息,更加精确指明当前的活动能响应Intent中可能带有的category
         * 特别注意:    只有action和category标签的内容同时匹配上Intent指定的action和category时,这个活动才能响应Intent
        * */

        Button button_Panel = (Button)findViewById(R.id.buttonPanel);

        button_Panel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                /*
                * 使用Intent构造函数,将action的字符串传入,表明想要启动com.example.reactest.ACTION_START这个action活动
                * 前面说了要只有action和category标签的内容同时匹配上Intent指定的action和category时,这个活动才能响应Intent
                * 但是这里没有使用,也可以成功
                * 说明:   android.intent.category.DEFAULT是一种默认category
                * */
                Intent intent = new Intent("com.example.reactest.ACTION_START");
                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">

    <!--    只要创建任何资源都会在R文件中生成一个资源id
    在引用布局的时候会调用R.layout.(Activity)可以得到XXXX.xml布局中的id,然后将这个值传到setContentVies()方法即可-->

    <!--    wrap_content    当前宽度只要刚好包含里边的内容就行-->
    <!--    match_parent    当前元素和父元素一样宽 相配,相称    -->

    <Button
        android:id="@+id/buttonPanel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="1"
        tools:ignore="MissingConstraints" />

<!--    通过button触发Toast 通过onCreate方法中添加代码-->


</androidx.constraintlayout.widget.ConstraintLayout>

SecondActivity代码:

package com.example.reactest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_layout);

    }
}

second_layout.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--添加一个button      orientation 方向  vertical垂直的 -->

    <Button
        android:id="@+id/buttonPanel_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="第二个视图"
        tools:ignore="MissingConstraints" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值