Android Intent实现页面之间跳转
什么是Intent
Intent可以理解为信使(意图)
由Intent来协助完成Android各个组件之间的通讯
Intent实现页面逐渐的跳转
1.startActivity(inetnt)
2.startActivityForResult(intent, requestCode);
onAcitivtyResult(int requestCode, int resultCode, Intent data)
setResult(resultCode, data);
先创建两个xml文件firstactivity.xml和secondactivity.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" > <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="通过Intent实现页面之间的跳转\n通过按钮1实现无返回结果的页面跳转\n通过按钮2实现有返回结果的页面跳转" /> <Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮1" /> <Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮2" /> <TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="页面跳转后的结果将会显示在这里" /> </LinearLayout>
firstactivity.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" > <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击返回第一个Activity" /> <Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" /> </LinearLayout>
secondactivity.xml
在AndroidManifest.xml中注册这两个xml,并设置firstactivity.xml是启动的时候加载的xml。
<activity
android:name="com.example.intentdemo.FirstActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.intentdemo.SecondActivity"
android:label="@string/app_name" >
</activity>
FirstActivity.java的buttton1用于通过startActivity()方法实现跳转。
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent);
}
});
FirstActivity.java的buttton2用于通过startActivityWithResult()方法实现跳转。
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
int requestCode = 1; // 自己取的
startActivityForResult(intent, requestCode);
}
});
第二个Activity返回结果并通过finish()销毁自己。
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
int resultCode = 2;
Intent data = new Intent();
data.putExtra("data", "这是第二个Activity返回的结果");
setResult(resultCode, data);
finish();
}
});
}
第一个Activity通过onActivityResult()方法得到返回的结果并显示在TextView中。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == 2) {
textView.setText(data.getStringExtra("data"));
}
}
package com.example.intentdemo; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView; public class FirstActivity extends Activity { private Button button1;
private Button button2;
private TextView textView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.firstactivity); textView = (TextView) findViewById(R.id.textView1); button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() { @Override
public void onClick(View view) {
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent);
}
}); button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
int requestCode = 1; // 自己取的
startActivityForResult(intent, requestCode);
} });
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == 2) {
textView.setText(data.getStringExtra("data"));
}
}
}
FirstActivity.java
package com.example.intentdemo; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class SecondActivity extends Activity { private Button button; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactivity); button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() { @Override
public void onClick(View view) {
int resultCode = 2;
Intent data = new Intent();
data.putExtra("data", "这是第二个Activity返回的结果");
setResult(resultCode, data);
finish();
}
});
} }
SecondActivity.java
效果:

Android Intent实现页面之间跳转的更多相关文章
- Android Studio 使用Intent实现页面的跳转(带参数)
不管是在APP,还是在网站中,页面之间的跳转都是很常见的,本文主要讲一下在APP中,如何通过Intent实现页面的跳转. 不带参数: 写在MainActivity页面的代码: Intent inten ...
- Android Intent实现页面跳转
Intent可以来协助完成Android各个组件之间的通信 1:startActivity(intent); //直接启动 /* ...
- Android 安卓实现页面相互跳转并相互传递参数
一.对于两个页面之间相互传值,跳转的时候我们使用 startActivityForResult(intent,0),而不是startActivity(intent) 这个方法 第一个页面中在触发跳转的 ...
- PHP页面之间跳转方法总结
编程中,在页面之间进行跳转是必须的.这里列出了三种办法,供参考. 一.用HTTP头信息 也就是用PHP的HEADER函数.PHP里的HEADER函数的作用就是向浏览器发出由HTTP协议规定的本来应该通 ...
- js实现两个页面之间跳转参数传递
html在设计时,规定跳转地址后加"?"表示从此开始为跟随页面地址跳转的参数. 有时候,我们希望获得相应的跳转前页面里的内容,这时候我们就可以考虑将内容以参数形式放到地址中传过来, ...
- android入门:activity之间跳转,并且回传参数
介绍: 两个activity进行跳转,在跳转过程中,将message由MainActivity传递到secondActivity,并且当secondActivity退回至MainAct ...
- Android两个页面之间的切换效果工具类
import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; ...
- intent实现Activity之间跳转的各种传值
一.在Activity之间传递String类型的数据 传递 @Override public void onClick(View v) { String num1 = firstNum.getText ...
- C#中页面之间跳转方法比较
一直以来,各种跳转方法混用,浑浑噩噩没有仔细去了解过每个跳转方法的区别 1.<a herf="default.asp"></a> 超链接跳转 2.< ...
随机推荐
- C#写的工厂模式
program.cs file using System; using System.Collections.Generic; using System.Text; namespace Console ...
- Extjs Toolbar 当做弹出菜单
menuAlign: 'tl-tr', listeners: { mouseover: function(btn) { btn.toolb.showBy(btn,btn.menuAlign); } } ...
- KafkaStream时间戳问题CreateTime = -1引起的程序中断
Exception in thread "app-8835188a-e0a0-46da-ac2a-6820ec197628-StreamThread-1" org.apache.k ...
- QML vs WEB
UI领域, 如果是桌面应用,QML可以更快速.如果是手机UI,H5绝对占优. 移动设备已经为各行业的HMI的响应性和用户友好性设定了标准.汽车,医疗设备,工业自动化系统和消费电子产品制造商现在希望为其 ...
- Boostrap入门级css样式学习
1. 自适应网页设计 首先,在网页代码的头部,加入一行 viewport元标签.viewport是网页默认的宽度和高度, <meta name="viewport" cont ...
- 第二百八十八节,MySQL数据库-索引、limit分页、执行计划、慢日志查询
MySQL数据库-索引.limit分页.执行计划.慢日志查询 索引,是数据库中专门用于帮助用户快速查询数据的一种数据结构.类似于字典中的目录,查找字典内容时可以根据目录查找到数据的存放位置,然后直接获 ...
- ‘,’之后要留空格,如 Function(x, y, z)
‘,’之后要留空格,如 Function(x, y, z).如果‘;’不是一行的结束 符号,其后要留空格,如 for (initialization; condition; update). #inc ...
- Eclipse最经常使用快捷键总结
1. ctrl+shift+r:打开资源 这可能是全部快捷键组合中最省时间的了. 这组快捷键能够让你打开你的工作区中不论什么一个文件,而你仅仅须要按下文件名称或mask名中的前几个字母,比方appl ...
- 【Java 线程的深入研究4】ThreadPoolExecutor运转机制详解
hreadPoolExecutor机制 一.概述 1.ThreadPoolExecutor作为java.util.concurrent包对外提供基础实现,以内部线程池的形式对外提供管理任务执行,线程调 ...
- PHP中替换换行符方法总结
<?php header("content-type:text/html;charset=utf-8"); $str = "aaaa bbbb cccc dddd& ...