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#/WPF】调节图像的对比度(Contrast)
关于对比度: 调节对比度直观感受是,高对比度的图像明暗关系更明显,色彩更鲜艳:低对比度的图像表面像是蒙上一层灰,色彩不鲜艳. 需求: 制作一个面板,一个滑动条,拖动滑动条可以修改目标图片的对比度. 资 ...
- 在Window下安装解压版的mysql 5.7.11
今天由于要在windows下学习Kettle,因此在Windows下安装了mysql 5.7.11,本来是没什么大问题的,但是在启动服务时还是出了点问题,服务老是启动不了: (一)解压到安装路径: ...
- mysql 2013错误解决
今天,莫名其妙的来了个mysql 2013错误,导致无法登陆mysql gui工具,而且dos也进不去,提示ping 127.0.0.1,百度+google后: 这是在使用 mysql 的过程中,困扰 ...
- JavaScrip——练习(做悬浮框进一步:悬浮窗后缀悬浮窗【感觉这种方法比较麻烦】)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- openwrt内核配置选项添加
摘自:http://blog.csdn.net/weiniliuchao/article/details/50295527 增加内核配置选项 openwrt的.config文件中,关于内核的选项都是形 ...
- 一站式学习Wireshark(一):Wireshark基本用法
按照国际惯例,从最基本的说起. 抓取报文: 下载和安装好Wireshark之后,启动Wireshark并且在接口列表中选择接口名,然后开始在此接口上抓包.例如,如果想要在无线网络上抓取流量,点击无线接 ...
- Lua 中pairs与ipairs区别
local tmp_tab = {}; tmp_tab[]="lua"; tmp_tab[]="hello" tmp_tab[]="aaa" ...
- Java NIO使用及原理分析 (一)(转)
最近由于工作关系要做一些Java方面的开发,其中最重要的一块就是Java NIO(New I/O),尽管很早以前了解过一些,但并没有认真去看过它的实现原理,也没有机会在工作中使用,这次也好重新研究一下 ...
- 測试Service
<strong><span style="font-size:18px;">自己定义Service:</span></strong> ...
- iOS彩票项目--第一天,自定义TabBar控制器和自定义TabBar,自定义导航控制器
一.环境配置,和项目层次搭建 二.自定义TabBar 项目中TabBar中的导航按钮美工给的图片太大,图片中包含了图片和文字.最主要的是TabBar上面的按钮图片尺寸是有规定的,当高度大于44的时候, ...