Activity组件:(一)通过显式意图和隐式意图来实现Activity间的跳转
一、通过显式意图来实现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间的跳转的更多相关文章
- Android 显示意图和隐式意图的区别
意图在android的应用开发中是很重要的,明白了意图的作用和使用后,对开发会有很大帮助.如果没有把意图搞懂,以后开发应用会感觉缺些什么. 意图的作用: 1.激活组件 ...
- 转】C#接口-显式接口和隐式接口的实现
[转]C#接口-显式接口和隐式接口的实现 C#中对于接口的实现方式有隐式接口和显式接口两种: 类和接口都能调用到,事实上这就是“隐式接口实现”. 那么“显示接口实现”是神马模样呢? interface ...
- C# Interface显式实现和隐式实现
c#中对接口的实现方式有两种:隐式实现和显式实现,之前一直没仔细看过,今天查了些资料,在这里整理一下. 隐式实现的例子 interface IChinese { string Speak(); } p ...
- 多态设计 zen of python poem 显式而非隐式 延迟赋值
总结 1.python支持延迟赋值,但是给调用者带来了困惑: 2.显式而非隐式,应当显式地指定要初始化的变量 class Card: def __init__(self, rank, suit): s ...
- C# 数据类型转换 显式转型、隐式转型、强制转型
C# 的类型转换有 显式转型 和 隐式转型 两种方式. 显式转型:有可能引发异常.精确度丢失及其他问题的转换方式.需要使用手段进行转换操作. 隐式转型:不会改变原有数据精确度.引发异常,不会发生任何问 ...
- selenium-webdriver中的显式等待与隐式等待
在selenium-webdriver中等待的方式简单可以概括为三种: 1 导入time包,调用time.sleep()的方法传入时间,这种方式也叫强制等待,固定死等一个时间 2 隐式等待,直接调用i ...
- (java)selenium webdriver学习---三种等待时间方法:显式等待,隐式等待,强制等待
selenium webdriver学习---三种等待时间方法:显式等待,隐式等待,强制等待 本例包括窗口最大化,刷新,切换到指定窗口,后退,前进,获取当前窗口url等操作: import java. ...
- Java并发之显式锁和隐式锁的区别
Java并发之显式锁和隐式锁的区别 在面试的过程中有可能会问到:在Java并发编程中,锁有两种实现:使用隐式锁和使用显示锁分别是什么?两者的区别是什么?所谓的显式锁和隐式锁的区别也就是说说Synchr ...
- Scala 中的隐式转换和隐式参数
隐式定义是指编译器为了修正类型错误而允许插入到程序中的定义. 举例: 正常情况下"120"/12显然会报错,因为 String 类并没有实现 / 这个方法,我们无法去决定 Stri ...
- Scala 深入浅出实战经典 第61讲:Scala中隐式参数与隐式转换的联合使用实战详解及其在Spark中的应用源码解析
王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频.PPT.代码下载: 百度云盘:http://pan.baidu.com/s/1c0noOt ...
随机推荐
- raspberry pi-php-exec
遇到的问题是在树莓派上搭建的web服务器,想通过网页操控树莓派的gpio,网页是通过php实现的,通过php的exec函数调用写好的程序实现对gpio的操作,但是赖何没有效果,分析也知道是权限问题,最 ...
- 配置mysql时报错
配置mysql时无法启动此程序,因为计算机丢失MSVCR100.dll. 去https://cn.dll-files.com/下载相应的版本 复制MSVCR100.dll 粘贴到下面 32位系统: 复 ...
- java基础源码 (4)--reflect包-AnnotatedElement接口
接口:AnnotatedElement * Represents an annotated element of the program currently running in this * VM. ...
- 【剑指Offer】面试题05.替换空格
题目 请实现一个函数,把字符串 s 中的每个空格替换成"%20". 示例 1: 输入:s = "We are happy." 输出:"We%20are ...
- html 基础 (9.19 第八天)
一.HTML:超文本标记语言,是一种标签语言,不是编程语言,显示数据有双标签<body></body> 和单标签<img src=# / >, 标签大小写都可以通过 ...
- UVA - 11892 ENimEN(博弈)
题意:有n堆石子,两个人拿,拿走最后的石子的人赢,poopi先拿,条件是,每个人必须从另外一个人最后拿过的石子堆中取石子,若那堆石子被拿没了,才可以自由地拿其他堆.要求每次拿的石子数不能为0.问谁赢. ...
- ffmpeg 模块简介
FFmpeg 是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序.它包括了领先的音/视频编码库libavcodec 等.libavformat:用于各种音视频封装格式的生成和解析 ...
- cf 223B.Two Strings
神奇(%%题解) 题意:判断B串作为A串的子序列,不是不可以把A全部覆盖掉. 这样的话就是判断是不是A[i]最右匹配B的点和最左匹配B的点相交(重合)就好.(不重合的话B自然会空出中间一段,那么肯定不 ...
- Floyd--P1119 灾后重建
题目背景 B地区在地震过后,所有村庄都造成了一定的损毁,而这场地震却没对公路造成什么影响.但是在村庄重建好之前,所有与未重建完成的村庄的公路均无法通车.换句话说,只有连接着两个重建完成的村庄的公路才能 ...
- UVALive 5913 字典树
先输入n个字符串的字典,每个字符串的前缀+后缀可以组成新的合法字符串,但肯定是有重复的,问从给定的字符串,生成的所有可能的字符串为多少个 把前缀和后缀压入字典树,达到前缀和后缀的去重,首先的总和即为前 ...