一、通过显式意图来实现Activity间的跳转

显式意图是指在创建Intent对象时就指定接受者组件

 /**
* 下面是通过显式意图进行跳转,即明确写出要跳转到SecondActivity.class组件中去
*/
Intent intent =new Intent(this,SecondActivity.class);
intent.putExtra("account",account);
intent.putExtra("password",password);
startActivity(intent);

注意:创建Activity时要在manifests里进行静态注册,示例如下:

<activity android:name=".SecondActivity">

        </activity>

之后再要跳转到的界面接受Intent传递的内容

 //通过getIntent获取MainActivity传来的intent
Intent intent = getIntent();
String account = intent.getStringExtra("account");
String password = intent.getStringExtra("password");

点击登录按钮

二、通过隐式意图来实现Activity间的跳转

隐式意图就是通过intent过滤器来进行匹配跳转

 /**
* 下面是通隐式意图进行跳转,要在manifests里添加意图过滤
*/
Intent intent = new Intent();
intent.setAction("com.example.activitydemo.LoginInfo");
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.putExtra("account",account);
intent.putExtra("password",password);
startActivity(intent);

进行注册的同时添加intent过滤

<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="com.example.activitydemo.LoginInfo"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>

点击登录

三、原码

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activitydemo"> <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.example.activitydemo.LoginInfo"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application> </manifest>

MainActivity.java

package com.example.activitydemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity";
private EditText mAccount;
private EditText mPassword;
private Button mLogin; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); initView();
initListener();
} private void initListener() {
mLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//登录按钮被点击了
Log.d(TAG,"Login Click。。。");
handlerLogin();
}
});
} private void handlerLogin() {
//.trim()用于去空格
String account = mAccount.getText().toString().trim();
if (TextUtils.isEmpty(account)) {
Toast.makeText(this,"输入的账号为空",Toast.LENGTH_SHORT).show();
return;
} String password = mPassword.getText().toString().trim();
if (TextUtils.isEmpty(password)) {
Toast.makeText(this,"输入的密码为空",Toast.LENGTH_SHORT).show();;
}
//先要创建一个意图对象,然后通过StartActivity()来实现跳转
/**
* 下面是通过显式意图进行跳转,即明确写出要跳转到SecondActivity.class组件中去
*/
// Intent intent =new Intent(this,SecondActivity.class);
// intent.putExtra("account",account);
// intent.putExtra("password",password);
// startActivity(intent); /**
* 下面是通隐式意图进行跳转,要在manifests里添加意图过滤
*/
Intent intent = new Intent();
intent.setAction("com.example.activitydemo.LoginInfo");
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.putExtra("account",account);
intent.putExtra("password",password);
startActivity(intent);
} private void initView() {
mAccount = (EditText) this.findViewById(R.id.account);
mPassword = (EditText) this.findViewById(R.id.password);
mLogin = (Button) this.findViewById(R.id.login);
}
}

activity_main.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"
android:orientation="vertical"
tools:context=".MainActivity"> <TextView
android:layout_width="wrap_content"
android:text="登录"
android:textSize="30sp"
android:layout_gravity="center"
android:layout_height="wrap_content"> </TextView> <TextView
android:layout_width="wrap_content"
android:text="账号:"
android:textSize="25sp"
android:layout_height="wrap_content"> </TextView> <EditText
android:id="@+id/account"
android:layout_width="match_parent"
android:layout_height="wrap_content"> </EditText> <TextView
android:layout_width="wrap_content"
android:text="密码:"
android:textSize="25sp"
android:layout_height="wrap_content"> </TextView>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:inputType="textPassword"
android:layout_height="wrap_content"> </EditText>
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:text="登录"
android:layout_height="wrap_content"> </Button>
</LinearLayout>

SecondActivity.java

package com.example.activitydemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView; import androidx.annotation.Nullable; public class SecondActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second); TextView info =(TextView) this.findViewById(R.id.info);
//通过getIntent获取MainActivity传来的intent
Intent intent = getIntent();
String account = intent.getStringExtra("account");
String password = intent.getStringExtra("password"); info.setText("您的账号为:"+account+"您的密码为:"+password);
}
}

activity_second_.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView
android:layout_width="match_parent"
android:text="登录信息如下:"
android:layout_marginTop="20dp"
android:textSize="20sp"
android:layout_height="wrap_content"> </TextView>
<TextView
android:id="@+id/info"
android:layout_width="match_parent"
android:textSize="25sp"
android:text=""
android:layout_height="wrap_content"> </TextView>
</LinearLayout>

Activity组件:(一)通过显式意图和隐式意图来实现Activity间的跳转的更多相关文章

  1. Android 显示意图和隐式意图的区别

    意图在android的应用开发中是很重要的,明白了意图的作用和使用后,对开发会有很大帮助.如果没有把意图搞懂,以后开发应用会感觉缺些什么.        意图的作用:        1.激活组件   ...

  2. 转】C#接口-显式接口和隐式接口的实现

    [转]C#接口-显式接口和隐式接口的实现 C#中对于接口的实现方式有隐式接口和显式接口两种: 类和接口都能调用到,事实上这就是“隐式接口实现”. 那么“显示接口实现”是神马模样呢? interface ...

  3. C# Interface显式实现和隐式实现

    c#中对接口的实现方式有两种:隐式实现和显式实现,之前一直没仔细看过,今天查了些资料,在这里整理一下. 隐式实现的例子 interface IChinese { string Speak(); } p ...

  4. 多态设计 zen of python poem 显式而非隐式 延迟赋值

    总结 1.python支持延迟赋值,但是给调用者带来了困惑: 2.显式而非隐式,应当显式地指定要初始化的变量 class Card: def __init__(self, rank, suit): s ...

  5. C# 数据类型转换 显式转型、隐式转型、强制转型

    C# 的类型转换有 显式转型 和 隐式转型 两种方式. 显式转型:有可能引发异常.精确度丢失及其他问题的转换方式.需要使用手段进行转换操作. 隐式转型:不会改变原有数据精确度.引发异常,不会发生任何问 ...

  6. selenium-webdriver中的显式等待与隐式等待

    在selenium-webdriver中等待的方式简单可以概括为三种: 1 导入time包,调用time.sleep()的方法传入时间,这种方式也叫强制等待,固定死等一个时间 2 隐式等待,直接调用i ...

  7. (java)selenium webdriver学习---三种等待时间方法:显式等待,隐式等待,强制等待

    selenium webdriver学习---三种等待时间方法:显式等待,隐式等待,强制等待 本例包括窗口最大化,刷新,切换到指定窗口,后退,前进,获取当前窗口url等操作: import java. ...

  8. Java并发之显式锁和隐式锁的区别

    Java并发之显式锁和隐式锁的区别 在面试的过程中有可能会问到:在Java并发编程中,锁有两种实现:使用隐式锁和使用显示锁分别是什么?两者的区别是什么?所谓的显式锁和隐式锁的区别也就是说说Synchr ...

  9. Scala 中的隐式转换和隐式参数

    隐式定义是指编译器为了修正类型错误而允许插入到程序中的定义. 举例: 正常情况下"120"/12显然会报错,因为 String 类并没有实现 / 这个方法,我们无法去决定 Stri ...

  10. Scala 深入浅出实战经典 第61讲:Scala中隐式参数与隐式转换的联合使用实战详解及其在Spark中的应用源码解析

    王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频.PPT.代码下载: 百度云盘:http://pan.baidu.com/s/1c0noOt ...

随机推荐

  1. 七十四、SAP中内表的4中删除方法

    一.代码如下 二.效果如下 *&---------------------------------------------------------------------* *& Re ...

  2. 留学Essay写作中常见的两类要求词盘点

    写essay的时候,我们会常常因为各式各样的要求词而头疼:discuss,describing,evaluate,explain,等等,他们之间有何区别?如果你在思考这个问题,那么这篇文章就是为你写的 ...

  3. 实验吧-隐写术-黑与白(二)(反转+五笔+Image steganography)

    反转有二:颜色反转.文件名反转 文件名这么乱,毫无规律,好奇怪,进行反转后发现是:steganography(就是隐写术的意思),这还是个图片文件,有一款工具正好叫Image steganograph ...

  4. 实验吧-隐写术-guess(outguess)

    给了一张图片: 看了看属性,没啥东西. 根据题目:guess guess guess不出你就out了 猜测是用outguess,于是上linux,进入outguess文件夹,执行命令:outguess ...

  5. PHP操作MYSQL数据库(10.11 第十九天)

    一.连接及断开数据库 1.使用mysqli 扩展(推荐),只针对mysql数据库 面向对象的方式 $con = new mysqli(ip,user,password); if($con->co ...

  6. HDU 5285:wyh2000 and pupil

    wyh2000 and pupil  Accepts: 93  Submissions: 925  Time Limit: 3000/1500 MS (Java/Others)  Memory Lim ...

  7. 《新标准C++程序设计》1.7-1.10(C++学习笔记2)

    1.内联函数(inline关键字) eg.inline int Max(int a,int b) { if(a>b) return a; return b; } 当编译器处理调用内联函数的语句时 ...

  8. bne 1b 汇编含义

    汇编指令中 bne label 这条指令有以下两种特别的写法:bne 1b, bne 1f. bne 1b 指的是 backward,倒退寻找标号为 1 的地方并跳转. 同样也有 bne 1f,值得是 ...

  9. css代码实现列表等宽

    实现上面的手机页面,我们会遇到一个自适应的问题,但是手机页面的屏幕大小不一致,自适应的问题不是百分比可以好好解决的,我采用下面的布局:display:flex; <!DOCTYPE html&g ...

  10. GWCTF 2019]我有一个数据库

    0x00 知识点 phpMyadmin(CVE-2018-12613)后台任意文件包含漏洞 影响版本:4.8.0--4.8.1 payload:/phpmyadmin/?target=db_datad ...