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.< ...
随机推荐
- iOS导航栏背景,标题和返回按钮文字颜色
在iOS7下,默认导航栏背景,颜色是这样的,接下来我们就进行自定义,如果你仅仅是更改一下背景和颜色,代码会很简单,不需要很复杂的自定义View来替代leftBarItem 更改导航栏的背景和文字Col ...
- WEB只能输入固定的字符
<head runat="server"> <title></title> <script type="text/javascr ...
- 【Unity】用代码给按钮动态添加点击事件
问题:多数情况下用UGUI的Button控件身上的OnClick()列表可以指明该按钮点击后触发的回调.现在想要调用自定义脚本里的方法,当这个脚本挂在Button所属的Canvas身上时,传入Canv ...
- RP2833 FPGA对应串口标识
U41 FPGA-TXD0 /dev/ruart0 FPGA-RXD0 U40 FPGA-TXD1 /dev/ruart1 FPGA-RXD1
- android学习日记01--综述
开个博客,写点关于Android的知识,希望温故而知新吧! 一.总体框架 先上一张google提供官方的Android框架图: Android系统架构由5部分组成,分别是:Linux Kernel.A ...
- Unable to resolve target 'android-20'
使用eclipse编写android的app时,出现错误:Unable to resolve target 'android-20'. 参看链接: http://blog.csdn.net/u0134 ...
- dm8127-内存分配
在前天一直完车辆捕获算法和车牌识别算法之后,算法移植告一段落,五月份以来,总算有点欣慰了,可是cmos采集视频有点问题,主要是前端采集不是我接手,嵌入式部门的小宋和小李负责,据说是20多万没了图纸,防 ...
- Oracle-05-SQL语句概述、分类&SQL*PLUS概述(初识insert,desc,list,r,del,a,c,n等命令)
一.SQL语句概述 (1)SQL全程是"结构化查询语言(Structured Query Language)". SQL是大多数主流数据库系统採用的标准查询语言. (2)SQL语句 ...
- LabVIEW中数组的自动索引
我们在LabVIEW里面使用While或者是For循环结构的时候,就会发现每一个循环中在它们的循环结构的边界都可以自动完成一个数组元素的索引或累积.LabVIEW中循环结构的这种能力就叫做自动索引(A ...
- LaTeX公式
在学习机器学习中会接触到大量的数学公式,所以在写博客是会非常的麻烦.用公式编辑器一个一个写会非常的麻烦,这时候我们可以使用LaTeX来插入公式. 写这篇博文的目的在于,大家如果要编辑一些简单的公式,就 ...