Android入门之Intent在Activity中的用法--显示Intent

Android

Android入门之Intent在Activity中的用法–显示Intent


前言

Intent有两种跳转方式,使用显示Intent和隐式Intent


一、Intent主要属性

Intent主要属性有action、data、Category、Extras

1、action

(1)用于Activity的一些标准动作

ACTION_CALL启动电话
ACTION_EDIT显示数据编辑界面
ACTION_MAIN启动项目的初始界面
ACTION_SYNC同步服务器和移动设备的数据

(2)用于广播接收者的动作

ACTION_BATTERY_LOW警告电池电池电量低
ACTION_HEADSET_PLUG耳机插入/拔掉设备
ACTION_SCREEN_ON屏幕已打开
ACTION_TIMEZONE_CHANGED时区设置改变

2、data

  • 指向手机通讯录联系人的URI:content://contacts/l
  • 打电话tel:URI
  • 访问网络http://URI
  • 内容提供者提供的数据为content:URIs

自定义action动作:

com.fjr.mproject.SHOW_COLOR,并可以定义相应的Activity来处理我们的自定义动作
(fjr是包名)

3、category

CATEGORY_DEFAULT,默认的category
CATEGORY_BROWSABLE,该Activity能被浏览器安全调用
CATEGORY_HOME,设置该Activity随系统启动而运行
CATEgORY_LAUNCHER,Intent的接收者应该在Launcher中作为顶级应用而出现
CATEDORY_PREFERENCE,该Activity是参数面板

二、使用显示Intent

显示、intent直接使用组件的名称定义目标组件,这种方式很直接

在这里插入图片描述Intent有多个构造函数的重载
eg:Intent(Context packageContext,Class<?>cls)
此构造函数接收两个参数:
第一个参数Context要求提供一个启动Activity的上下文
第二个参数Class用于指定想要启动的目标Activity
通过这个构造函数就可以构建Intent的“意图”

1.例子

从第一页跳转到第二页

(1)实现效果

在这里插入图片描述

在这里插入图片描述

(2)实现步骤

第一页xml

<?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"
    android:gravity="center">

    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第一页"
        android:textSize="30sp"/>

    <Button
        android:id="@+id/button_to2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="打开第二页" />

</LinearLayout>

第二页xml

<?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"
    android:gravity="center">

    <TextView
        android:id="@+id/textview2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第二页"
        android:textSize="30sp"/>
</LinearLayout>

编写意图
从Activity[活动1->活动2]

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent to2=new Intent(MainActivity.this,SecondActivity.class);;
                startActivity(to2);//start
            }
        });

2.页面源码

package com.example.intent;

import androidx.appcompat.app.AppCompatActivity;

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

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

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent to2=new Intent(MainActivity.this,SecondActivity.class);
                startActivity(to2);//start
            }
        });
    }

}

总结

以上就是今天要讲的内容,本文仅仅简单介绍了Intent在Activity中的显示Intent的用法。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值