Activity详解二 activity数据传递
首先看效果图:

1.Bundle类的作用
Bundle类用作携带数据,它类似于Map,用于存放key-value名值对形式的值。相对于Map,它提供了各种常用类型的putXxx()/getXxx()方法,如:putString()/getString()和putInt()/getInt(),putXxx()用于往Bundle对象放入数据,getXxx()方法用于从Bundle对象里获取数据。Bundle的内部实际上是使用了HashMap<String, Object>类型的变量来存放putXxx()方法放入的值。简单地说,Bundle就是一个封装好的包,专门用于导入Intent传值的包。
2.为Intent附加数据的两种写法
第一种写法,用于批量添加数据到Intent:
Intentintent = new Intent();
Bundle bundle = new Bundle();//该类用作携带数据
bundle.putString("name","Alice");
intent.putExtras(bundle);//为意图追加额外的数据,意图原来已经具有的数据不会丢失,但key同名的数据会被替换
第二种写法:这种写法的作用等价于上面的写法,只不过这种写法是把数据一个个地添加进Intent,这种写法使用起来比较方便,而且只需要编写少量的代码。
Intent intent = new Intent();
intent.putExtra("name","XXX");
那么,这两种方法有什么区别呢?
完全没有区别。当你调用putExtras()方法时,所传入的Bundle会被转化为Intent的键值(别忘了Intent也以键值模式转载数据)。
那么,现在看看如何将Intent和Bundle取出来。
方法很简单,直接使用this.getIntent()就可以得到传来的Intent,然后在这个Intent的基础上调用getExtras()就可以得到Bundle。然后这个Bundle你想要什么得到什么就get什么。
比如String str=bundle.getString("USERNAME"); 就是得到键为“USERNAME”的字符串,int num=bundle.getInt("Number");就是得到键为“Number”的整型。
android中的组件间传递的对象一般实现Parcelable接口,当然也可以使用java的Serializable接口,前者是android专门设计的,效率更高,下面我们就来实现一个Parcelabel。
1. 创建一个类实现Parcelable接口,具体实现如下:
public class ParcelableData implements Parcelable{
private String name;
private int age;
public ParcelableData(){
name = "guest";
age = 20;
}
public ParcelableData(Parcel in){
//顺序要和writeToParcel写的顺序一样
name = in.readString();
age = in.readInt();
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public int getAge(){
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeString(name);
dest.writeInt(age);
}
public static final Parcelable.Creator<ParcelableData> CREATOR = new Parcelable.Creator<ParcelableData>() {
public ParcelableData createFromParcel(Parcel in) {
return new ParcelableData(in);
}
public ParcelableData[] newArray(int size) {
return new ParcelableData[size];
}
};
}
2. 通过下面的方法发送对象。Bundle类也实现了Parcelable接口,一般在android中我们是通过Bundle来封装数据并进行传送的。
Intent intent = new Intent();
intent.setClass(this, SubActivity.class);
// 直接添加
//intent.putExtra("MyData", new ParcelableData());
// 通过Bundle
Bundle bundle = new Bundle();
bundle.putString("MyString", "test bundle");
bundle.putParcelable("MyData", new ParcelableData());
intent.putExtras(bundle);
startActivity(intent);
3. 下面的接收对象的方法。
//ParcelableData parcelableData = getIntent().getParcelableExtra("MyData");
Bundle bundle = getIntent().getExtras();
ParcelableData parcelableData = bundle.getParcelable("MyData");
String testBundleString = bundle.getString("MyString");
Log.v("string=", testBundleString);
Log.v("name=", parcelableData.getName());
Log.v("age=", ""+parcelableData.getAge());
3 DEMO下载
activity代码:
package mm.shandong.com.testbundle; import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast; import java.util.ArrayList; import mm.shandong.com.testbundle.entity.Person; public class TestBundleActivity extends AppCompatActivity {
EditText editText1;
EditText editText2; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_bundle);
editText1 = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
}
///提交选择的地区,并把地区传递给TestBundleActivity3
public void submitRegion(View view) {
EditText editTextRegion = (EditText) findViewById(R.id.editTextRegion);
Intent intent = new Intent(this, TestBundleActivity3.class);
String region = editTextRegion.getText().toString();
if (!TextUtils.isEmpty(region)) {
intent.putExtra("region", region);
startActivity(intent);
} else {
Toast.makeText(this, "地区不能是空值", Toast.LENGTH_SHORT).show();
}
}
///把需要计算的两个值都是Integer类型,传入到TestBundleActivity1
public void calculte(View view) {
Intent intent = new Intent(this, TestBundleActivity1.class);
Bundle bundle = new Bundle();
String first = editText1.getText().toString();
String second = editText2.getText().toString();
if (!TextUtils.isEmpty(first) && !TextUtils.isEmpty(second)) {
bundle.putInt("first", Integer.parseInt(first));
bundle.putInt("second", Integer.parseInt(second));
intent.putExtras(bundle);
startActivity(intent);
} else {
Toast.makeText(this, "数值不能是空", Toast.LENGTH_SHORT).show();
}
}
///传递Serializable对象到TestBundleActivity2
public void login(View view) {
EditText editTextName = (EditText) findViewById(R.id.editTextName);
EditText editTextCode = (EditText) findViewById(R.id.editTextCode);
Intent intent = new Intent(this, TestBundleActivity2.class);
Bundle bundle = new Bundle();
String name = editTextName.getText().toString();
String code = editTextCode.getText().toString();
if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(code)) {
Person person = new Person();
person.setName(name);
person.setCode(code);
bundle.putSerializable("person", person);
intent.putExtras(bundle);
startActivity(intent);
} else {
Toast.makeText(this, "姓名编号不能是空", Toast.LENGTH_SHORT).show();
}
} }
本人微博:honey_11
Demo下载
最后,以上例子都来源与安卓无忧,请去应用宝或者豌豆荚下载:例子源码,源码例子文档一网打尽
Activity详解二 activity数据传递的更多相关文章
- Activity详解四 activity四种加载模式
先看效果图: 1概述 Activity启动方式有四种,分别是: standard singleTop singleTask singleInstance 可以根据实际的需求为Activity设置对应的 ...
- Xamarin android 之Activity详解
序言: 上篇大概的讲解了新建一个android的流程.今天为大家带来的是Activity详解,因为自己在开发过程中就遇到 好几次坑,尴尬. 生命周期 和Java里头一样一样的,如图 图片来源于网上哈, ...
- 详解Android Activity启动模式
相关的基本概念: 1.任务栈(Task) 若干个Activity的集合的栈表示一个Task. 栈不仅仅只包含自身程序的Activity,它也可以跨应用包含其他应用的Activity,这样有利于 ...
- [安卓基础] 009.组件Activity详解
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- 详解Android中的四大组件之一:Activity详解
activity的生命周期 activity的四种状态 running:正在运行,处于活动状态,用户可以点击屏幕,是将activity处于栈顶的状态. paused:暂停,处于失去焦点的时候,处于pa ...
- 【Android】详解Android Activity
目录结构: contents structure [+] 创建Activity 如何创建Activity 如何创建快捷图标 如何设置应用程序的名称.图标与Activity的名称.图标不相同 Activ ...
- PopUpWindow使用详解(二)——进阶及答疑
相关文章:1.<PopUpWindow使用详解(一)——基本使用>2.<PopUpWindow使用详解(二)——进阶及答疑> 上篇为大家基本讲述了有关PopupWindow ...
- Android 布局学习之——Layout(布局)详解二(常见布局和布局参数)
[Android布局学习系列] 1.Android 布局学习之——Layout(布局)详解一 2.Android 布局学习之——Layout(布局)详解二(常见布局和布局参数) 3.And ...
- LigerUI之Grid使用详解(三)——字典数据展示
一.问题概述 在开发web信息管理系统时,使用Web前端框架可以帮助我们快速搭建一组风格统一的界面效果,而且能够解决大多数浏览器兼容问题,提升开发效率.在关于LigerGrid的前两篇的内容里,给大家 ...
随机推荐
- 搞清arguments,callee,caller
arguments是什么? arguments是函数调用时,创建的一个类似的数组但又不是数组的对象,并且它存储的是实际传递给函数的参数,并不局限于函数声明的参数列表哦. 尼玛,什么意思? 写个demo ...
- MySQL学习笔记之MySQL安装详解
前言 虽然现在NoSQL发展迅速,但MySQL还是非常受欢迎的,成千上万的公司依旧采用LAMP OR LNMP的搭配来进行开发,因此MYSQL的学习还是有一定的必要. 安装环境:Windows 7,需 ...
- Ubuntu杂记——Ubuntu自带拼音输入发杂乱不堪
打开终端,用管理员权限输入ibus-daemon -drx,重启即可
- 项目安排(离散化+DP)
题目来源:网易有道2013年校园招聘面试二面试题 题目描述: 小明每天都在开源社区上做项目,假设每天他都有很多项目可以选,其中每个项目都有一个开始时间和截止时间,假设做完每个项目后,拿到报酬都是不同的 ...
- 关于IE6的PNG图像透明使用AlphaImageLoader的缺点
PNG32的alpha透明效果在IE6下会出现bug,出现灰色背景.而目前的解决方案就是 IE提供的滤镜.需要注意的是滤镜并不是对原图片进行修改,而是对相应的html元素进行 修改.所以在一个html ...
- NET开发学习项目资源
最近在整理资料时发现自己当初学习NET的一些项目资源,一直放在硬盘里不如拿来分享给初学者学习还是不错的. 项目代码为<精通ASP.NET20+SQL Server2005项目开发>书中源码 ...
- Mybatis-mapper-xml-基础
今天学习http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html.关于mapper.xml的sql语句的使用. 项目路径:https://github.c ...
- 使用AutoMapper进行对象间映射
在开发过程中,难免遇到下面这种情况:两个(或多个)对象所拥有的大多数属性是重复的,我们需要在对象间进行映射(即将一个对象的属性值赋给另一个对象.通常我们可以进行如下操作: A a=new A(); a ...
- 《C#并发编程经典实例》笔记
1.前言 2.开宗明义 3.开发原则和要点 (1)并发编程概述 (2)异步编程基础 (3)并行开发的基础 (4)测试技巧 (5)集合 (6)函数式OOP (7)同步 1.前言 最近趁着项目的一段平稳期 ...
- FPGA的引脚VCCINT 、VCCIO VCCA
首先是看到FPGA在配置的时候有三种不同的电VCCINT .VCCIO VCCA,于是就查了下有什么不同: FPGA一般会有许多引脚,那它们都有什么用呢? VCCINT为施加于 FPGA 内核逻辑的电 ...