一、通过显式意图来实现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. springcloud--ribbo(负载均衡)

    ribbo:是Netflix公司开源的一个负载均衡的项目,是一个客户端负载均衡器,运行在客户端上. 实际运用案例(基于springcloud入门案例): 一.新建Module:springcloud- ...

  2. 启用root关闭客人会话

    1.位root用户设置密码: sudo passwd root 2.修改配置文件/usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf(先备份) 添加如下在文 ...

  3. 043-PHP简单获得一个类对应的反射信息

    <?php // 简单获得一个类对应的反射信息 class demo{ CONST CON_STR = '123456'; public $str_1; private $str_2; prot ...

  4. 038-PHP向返回的闭包函数实例中,传递外部变量参数

    <?php # 向返回的闭包函数实例中,传递外部变量参数 # 直接调用将不会输出$txt的内容 function demo(){ $txt = '我爱PHP'; # 1.function()内的 ...

  5. > 1> 2> &> /dev/null Linux重定向输出

    编译模拟器的 LINK 阶段产生了大量错误信息,定位不到第一行,所以将错误重定向到了一个文件: scons build/X86_VI_hammer_GPU/gem5.opt --default=X86 ...

  6. ZOJ - 3123 Subsequence (滑动窗口)

    题意:给定N个数,求和大于等于S的最短连续子序列的长度. 分析:滑动窗口即可.两种写法. 1. #include<cstdio> #include<cstring> #incl ...

  7. 每天一点点之laravel框架开发 - Laravel5.6去除URL中的index.php

    在项目routes/web.php文件中添加了自定义的路由后,访问localhost/index.php/aaa,可以正常访问,但是去掉index.php后,提示404 Not Found 1. 按照 ...

  8. Vue 获取时间戳返回自定义时间格式

    直接在Vue全局函数定义: Vue.prototype.padLeftZero = function(str) { return ('00' + str).substr(str.length); }; ...

  9. PHP常用的数学函数和字符串函数

    PHP常用函数总结 数学函数 1.abs(): 求绝对值 $abs = abs(-4.2); //4.2 数字绝对值数字 2.ceil(): 进一法取整 echo ceil(9.999); // 10 ...

  10. python基础【2】——python数据类型之字符串

    python数据类型-字符串 一. 字符串的表示方法(str) 作用: 记录文本信息 表示方法:' ' 单引号 " "双引号 ''' '''三单引号 ""&qu ...