Android学习总结——Activity之间传递参数
一.在 Activity 之间传递简单数据
二.在 Activity 之间传递复杂数据
三.在 Activity 之间传递自定义值对象
Intent intent=new Intent(MainActivity.this,AnotherActivity.class);
//加入参数,传递给AnotherActivity
intent.putExtra("data","我是传过来的参数");
startActivity(intent);
getIntent().getStringExtra("data");
二.在 Activity 之间传递复杂数据
传递数据包Bundle
Intent intent=new Intent(MainActivity.this,AnotherActivity.class); Bundle b=new Bundle();
b.putString("name","小明");
b.putInt("age",);
b.putChar("sex",'男'); intent.putExtras(b);
startActivity(intent);
获取数据包Bundle
Intent i=getIntent();
Bundle data=i.getExtras(); TextView tv=(TextView)findViewById(R.id.tv);
tv.setText(String.format("name="+data.getString("name")+",age="+data.getInt("age")+",sex="+data.getChar("sex")+",score="+""));
三.在 Activity 之间传递自定义值对象
所谓的值对象就是自定义的有数据类型的对象,在实际使用当中传递值对象比较实用,所以这里我将着重总结一下这里。
我们新建一个数据类型Student:
这里实现了Serializable这个接口,下文中将详细讲解。
public class Student implements Serializable{
private String name;
private String sex;
private int age;
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;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Student(String name,String sex,int age){
this.name=name;
this.sex=sex;
this.age=age;
}
}
当不实现这个接口,直接把数据传给Intent时,会提示出错。

这里有两种解决的方法,一种是让Student实现java内置的用于序列化的一个接口Serializable,另一种是Android提供的用于序列化的一个接口Parcelable,下面我们一个一个来看:
1.首先让Student implements Serializable接口:
主Activity中:
intent.putExtra("Student",new Student("小明","男",20));
目标Activity中:
Intent i=getIntent();
Student student= (Student) i.getSerializableExtra("Student");
//如下方式即可获取Student的属性值
String name=student.getName();
String sex=student.getSex();
int age=student.getAge();
这个方法效率比较低,所以Android提供了一个专门用于序列化的接口Parcelable,下面就来简单说说这个接口。
2.Parcelable接口
当实现这个接口之后要求我们实现这两个方法,so,实现就好。

这里需要我们手动的去写这些个东东,这是因为它没有全自动化去序列的机制。
public class Student implements Parcelable {
private String name;private int age;
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;
}
public Student(String name,int age){
this.name=name;
this.age=age;
}
@Override
public int describeContents() {
return ;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
//将这两条数据保存起来用于方便传递
parcel.writeString(getName());
parcel.writeInt(getAge());
}
public static final Creator<Student> CREATOR=new Creator<Student>() {
@Override
public Student createFromParcel(Parcel parcel) {
return new Student(parcel.readString(),parcel.readInt());
}
@Override
public Student[] newArray(int i) {
return new Student[i];
}
};
}
主Activity中:
intent.putExtra("Student",new Student("小明",));
目标Activity中:
Intent i=getIntent();
Student student=i.getParcelableExtra("Student");
//如下方式即可获取Student的属性值
String name=student.getName();
String sex=student.getSex();
int age=student.getAge();
就这两种接口而言,Parcelable接口更快,但是很多地方需要自己写,对于像我这样的新手菜鸟来说消化它也是够费劲的,希望有大神能给点Android学习路上的经验和建议!!

Android学习总结——Activity之间传递参数的更多相关文章
- 【Android 复习】 : Activity之间传递数据的几种方式
在Android开发中,我们通常需要在不同的Activity之间传递数据,下面我们就来总结一下在Activity之间数据传递的几种方式. 1. 使用Intent来传递数据 Intent表示意图,很多时 ...
- Android学习之Activity之间的数据传递
Activity与Activity之间很多情况下都需要进行数据的传递,下面就用几个简单的例子来看一下. (一).一个Activity启动另一个Activity并将数据传递到这个Activity当中 思 ...
- Activity之间传递参数(四)
--------siwuxie095 获取Activity的返回参数 1.首先修改两个布局文件,都修改为 LinearLayout 布局, 添加orientation属性为:vertical. (1) ...
- 在Activity之间传递参数(四)
获取Activity的返回参数(在参数(三)User的例子的基础上实现): 1.activity_the_aty.xml文件:<EditText android:id="@+id/ed ...
- Activity之间传递参数(三)
------siwuxie095 传递值对象,即自定义的有数据类型的对象 1.首先 new 一个 class:User,用于创建自定义对象,同时右键 Generate 出 Constructor.se ...
- Activity之间传递参数(二)
------siwuxie095 传递数据包 1.传递数据包要用到Bundle,MainActivity.java中: package com.siwuxie095.sendargs; import ...
- Activity之间传递参数(一)
-------siwuxie095 传递简单数据 (1)首先创建一个项目:SendArgs (2)选择API:21 Android 5.0 (3)选择 Empty Activity (4)默认 (5) ...
- 在Activity之间传递参数(一)
准备: 一.创建主界面:activity_main.xml文件中<Button android:text="启动另一个Activity" android:id="@ ...
- 在Activity之间传递参数(三)——serializable和parcelable的区别
传递值对象: 一.serializable实现:简单易用 serializable的迷人之处在于你只需要对某个类以及它的属性实现Serializable 接口即可.Serializable 接口是一种 ...
随机推荐
- LeeCode-Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 /** * Definition for a ...
- 【LeetCode练习题】Pow(x, n)
Pow(x, n) Implement pow(x, n). 计算x的n次方. 解题思路: 考虑到n的值会很大,而且可为正可为负可为0,所以掉渣天的方法就是用递归了. 对了,这题也在<剑指off ...
- APPCAN学习笔记004---AppCan与Hybrid,appcan概述
APPCAN学习笔记004---AppCan与Hybrid,appcan概述 技术qq交流群:JavaDream:251572072 本节讲了appcan的开发流程,和开发工具 笔记不做具体介绍了,以 ...
- java值传递
Java使用按值传递的函数调用方式,这往往使我感到迷惑.因为在基础数据类型和对象的传递上,我就会纠结于到底是按值传递,还是按引用传递.其实经过学习,Java在任何地方,都一直发挥着按值传递的本色. 首 ...
- Linux SSH 远程操作与传送文件
操作系统:centos 6.5 x64 一.远程连接:在进行linux 的 ssh远程操作前,一定要确认linux 是否安装了 openssh-clients,为了方便起见,一般用yum安装即可:# ...
- 安装sql server 2008 management studio时,提示升级VS2008 到 SP1
安装sql server 2008 management studio时,提示错误:此计算机上安装了 Microsoft Visual Studio 2008 的早期版本.请在安装 SQL Serve ...
- 使用bulkCopy心得
最近一直在到excel导入,无意中发现Bulk Insert 批量导入,于是研究了一下,在测试的时候一直有问题,然后找度娘帮忙,说新增DataTable数据结构的时候,每个列要与数据库设计时字段对应, ...
- Email Cover Letter Format
http://jobsearch.about.com/od/sampleletters/ig/Sample-Letter-Formats/Email-Message-Format.htm Copy ...
- 将json的时间格式转换成正常的时间格式
/** * 对Date的扩展,将 Date 转化为指定格式的String * 月(M).日(d).12小时(h).24小时(H).分(m).秒(s).周(E).季度(q) 可以用 1-2 个占位符 * ...
- Visual Studio 使用技巧
整理备用: 1. 键入prop后,连续按两下tab, 可以自动生成属性,然后输入类型和名称. 类似的还有: propg, 生成private set的属性 propfull,生成私有字段,和相应属性 ...