Android(java)学习笔记89:Bundle和Intent类使用和交互
1. Bundle 和 Intent:
Bundle只是一个信息的载体 将内部的内容以键值对组织 ,Intent负责Activity之间的交互自己是带有一个Bundle的。Intent.putExtras(Bundle bundle)直接将Intent的内部Bundle设置为参数里的bundle,Intent.getExtras()直接可以获取Intent带有的Bundle.
Intent携带了Bundle数据,Bundle是一种数据包裹(打包数据),利用Intent机制通过Bundle数据进行不同Activity通信。
两个activity之间的通讯可以通过bundle类来实现,做法就是:
(1)新建一个bundle类
Bundle mBundle = new Bundle();
(2)bundle类中加入数据(key -value的形式,另一个activity里面取数据的时候,就要用到key,找出对应的value)
mBundle.putString("Data", "data from TestBundle");
(3)新建一个intent对象,并将该bundle加入这个intent对象
Intent intent = new Intent();
intent.setClass(TestBundle.this, Target.class);
intent.putExtras(mBundle);
完整代码如下:
AndroidManifest.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tencent.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".TestBundle"
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=".Target"></activity>
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>
两个类如下:intent从TestBundle类发起,到Target类。
类1:TestBundle类:
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 TestBundle extends Activity { private Button button1;
private OnClickListener cl;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); button1 = (Button) findViewById(R.id.button1);
cl = new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(TestBundle.this, Target.class);
Bundle mBundle = new Bundle();
mBundle.putString("Data", "data from TestBundle");//压入数据
intent.putExtras(mBundle);
startActivity(intent);
}
};
button1.setOnClickListener(cl);
}
}
类2: Target
import android.app.Activity;
import android.os.Bundle; public class Target extends Activity{ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.target);
<span style="color:#ff6600;">Bundle bundle = getIntent().getExtras(); </span> //得到传过来的bundle
String data = bundle.getString("Data");//读出数据
setTitle(data); }
}
布局文件main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button"
android:id = "@+id/button1"
/>
</LinearLayout>
target.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/target"
/>
</LinearLayout>
String.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, TestBundle!</string>
<string name="app_name">测试Bundle用法</string>
<string name="button">点击跳转</string>
<string name="target">来到target activity</string>
</resources>
结果:

跳转结果:

Android(java)学习笔记89:Bundle和Intent类使用和交互的更多相关文章
- Android(java)学习笔记146:Bundle和Intent类使用和交互
Bundle只是一个信息的载体 将内部的内容以键值对组织 ,Intent负责Activity之间的交互自己是带有一个Bundle的.Intent.putExtras(Bundle bu ...
- 疯狂java学习笔记之面向对象(一) - 定义类、方法、构造器
Java面向对象 1.定义类 2.创建对象.调用方法 类和对象: 某一类对象的概念定义. 比如:人类 - 抽象出来的概念(不特指某个人) 对象 - 在类的概念下产生的一个实例,它就是一个对象了. ja ...
- java学习笔记(三):类和对象
创建对象 构造器 每一个类都有一个构造器. 如果我们不单独为一个类编写构造器那么 Java 的编译器将会给这个类建立一个默认的构造器. 每当一个新的对象被创建,至少一个构造器将会被调用. 构造器的一 ...
- Java学习笔记(七)——获取类中方法的信息,java的LinkedList
[前面的话] 在实际项目中学习知识总是最快和最有效的,既能够较好的掌握知识,又能够做出点东西,还是简单的知识总结,最近一直在总结笔记,写的东西还是比较水,希望慢慢可以写出一些干货. 学习过程中的小知识 ...
- java学习笔记37(sql工具类:JDBCUtils)
在之前的内容中,我们发现,当我们执行一条语句时,每新建一个方法,就要重新连接一次数据库,代码重复率很高,那么能不能把这些重复代码封装成一个类呢,我们学习方法时,就学习到方法就是为了提高代码的利用率,所 ...
- java学习笔记(五):公共类
什么是公共类,公共类就是和源文件名同名的类,举例来说:类的名称是 public class aaa{},那么源文件就应该是 aaa.java. 每个源文件中只能有一个公共类. 每个源文件可以有很多非公 ...
- Java学习笔记49(DBUtils工具类二)
上一篇文章是我们自己模拟的DBUtils工具类,其实有开发好的工具类 这里使用commons-dbutils-1.6.jar 事务的简单介绍: 在数据库中应用事务处理案例:转账案例 张三和李四都有有自 ...
- Java学习笔记48(DBUtils工具类一)
上一篇的例子可以明显看出,在增删改查的时候,很多的代码都是重复的, 那么,是否可以将增删改查封装成一个类,方便使用者 package demo; /* * 实现JDBC的工具类 * 定义方法,直接返回 ...
- Java学习笔记_22_Set接口的实现类
22.Set接口的实现类: Set接口存放的元素是无序的且不包括反复元素. 1>实现类HashSet: HashSet类依据元素的哈希码进行存放,取出时也能够依据哈希码高速找到.HashSet不 ...
随机推荐
- svg动画 animate
最近使用到svg的动画animate,简单总结下animate的主要属性: 1.定义:SVG 的animate 动画元素放在形状元素的内部,用来定义一个元素的某个属性如何踩着时点改变.在指定持续时间里 ...
- Codeforces Round #520 (Div. 2)B(贪心,数学)
#include<bits/stdc++.h>using namespace std;int mi[100007];int main(){ int cnt=0; int flag=0; i ...
- zoj3435(莫比乌斯反演)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3435 题意: 给出一个三维坐标 (x, y, z), 问该点与 ...
- Tour(dp)
Tour(dp) 给定平面上n(n<=1000)个点的坐标(按照x递增的顺序),各点x坐标不同,且均为正整数.请设计一条路线,从最左边的点出发,走到最右边的点后再返回,要求除了最左点和最右点之外 ...
- SpringBoot2.0 基础案例(04):定时任务和异步任务的使用方式
一.定时任务 1.基本概念 按照指定时间执行的程序. 2.使用场景 数据分析 数据清理 系统服务监控 二.同步和异步 1.基本概念 同步调用 程序按照代码顺序依次执行,每一行程序都必须等待上一行程序执 ...
- POJ1321-棋盘问题
题目链接:点击打开链接 Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和 ...
- AT2382 A or...or B Problem
传送门 还是看题解的啦 先考虑一个显而易见的结论:A和B二进制下最高的几位相同是没用的(设去掉的那些位之和为sum) 然后我们设\(d\)为二进制下从高位到低位第一位不相同的,\(k\)为B从高位到低 ...
- poi使用
1.首先需要下载Apache POI 打开poi下载的链接:http://poi.apache.org/download.html ,点击“The latest stable release is ...
- POJ 2068 NIm (dp博弈,每个人都有特定的取最大值)
题目大意: 有2n个人,从0开始编号,按编号奇偶分为两队,循环轮流取一堆有m个石子的石堆,偶数队先手,每个人至少取1个,至多取w[i]个,取走最后一个石子的队伍输.问偶数队是否能赢. 分析: 题目数据 ...
- Helvetic Coding Contest 2016 online mirror C1
Description One particularly well-known fact about zombies is that they move and think terribly slow ...