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.< ...
随机推荐
- DDD CQRS和Event Sourcing的案例:足球比赛
在12月11日新的有关DDD CQRS和Event Sourcing演讲:改变心态- 以更加面向对象视角看待业务领域建模中,作者以足球比赛football Match为案例说明传统编程方法和CQRS的 ...
- HBase shell 命令。
HBase shell 命令. 进入hbase shell console$HBASE_HOME/bin/hbase shell如果有kerberos认证,需要事先使用相应的keytab进行一下认证( ...
- eclipse安装使用教程
eclipse安装使用教程 很多人都知道要用eclipse来做java开发,但很多的新手朋友却不知道怎么下载和安装eclipse. 下面给你介绍一下怎么下载和安装eclipse来用于自己的学习或者项目 ...
- Bit operator: Left shift and Right shift (Signed or unsigned? )
No matter left shift or right shift, the result's sign should always be the same as its left operand ...
- archdexls主题设置每页显示游戏数目
archdexls主题,沒调整前,每页显示10个,这显然不够,尤其在搜狗浏览器上,由于这个主题只有触发下拉滚动条,才会自动在同一页面显示下一页,因此只显示10个甚至不能触发显示下一页这个动作. 原来设 ...
- JetBrains PyCharm 4.0.4 key
用户名 yueting3527 注册码 ===== LICENSE BEGIN ===== 93347-12042010 00001FMHemWIs"6wozMZnat3IgXKXJ 2!n ...
- JS中的数字比较
if(parseInt(current_index)!=parseInt(uls)){
- 【转】浅谈.net remoting 与webservice
1. .NET Remoting .NET Remoting是微软随.NET推出的一种分布式应用解决方案,被誉为管理应用程序域之间的 RPC 的首选技,它允许不同应用程序域之间进行通信(这里的通信可以 ...
- ffmpeg笔记——UDP组播接收总结
ffmpeg在avformat_open_input里面已经实现了UDP的协议,所以只需要设置好参数,将url传递进去就可以了. 和打开文件的方式基本一样: 01 AVCodecContext *pV ...
- Xcode/iOS: 如何判断代码运行在DEBUG还是RELEASE模式下?
原帖链接:http://stackoverflow.com/a/9063469 首先确定下项目的 Build Settings 是否已经设置过宏定义 DEBUG,如何看呢? 点击 Build Sett ...