Android Studio中显式Intent与隐式Intent

本文详细介绍了Android中Intent的概念及其在Android Studio中的应用,包括显式Intent和隐式Intent的创建与使用。显式Intent通过指定目标Activity的全类名进行跳转,而隐式Intent则通过定义action和category,让系统寻找合适的组件来响应。
摘要由CSDN通过智能技术生成

一、什么是Intent

Android中的Intent是一个非常重要且常用的类,可以用来在一个组件中启动App中的另一个组件或者是启动另一个App的组件,这里所说的组件指的是Activity、Service以及Broadcast。简而言之,Intent是一种协助应用间的交互与通讯的机制

二、准备工作

创建三个新的activity

1.图片显示activity

先把需要显示的图片(.png)放进drawable
在这里插入图片描述

xml文件

<?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=".SecondActivity"
    android:background="@drawable/background">
</LinearLayout>

2.时钟activity

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=".ThirdActivity">

    <TimePicker
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</androidx.constraintlayout.widget.ConstraintLayout>

3.日历显示activity

xml文件

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

    <DatePicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="33dp"
        android:layout_y="2dp"></DatePicker>

</AbsoluteLayout>

三、显式Intent

这里使用3种显式intent方法

①this指向当前的Activity,后面填写内容为需要跳转的Activity

intent.setClass(this,SecondActivity.class);

②this指向当前的Activity,后面是要跳转Activity的全类名

intent.setClassName(this,"com.example.activity.ThirdActivity");

③前面内容是要打开Activity的项目名,后面是要跳转项目中Activity的全类名

intent.setClassName("com.example.activity", "com.example.activity.FourthActivity");

java实现代码


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值