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 接口是一种 ...
随机推荐
- Eclipse中设置注释、日期等信息
在使用Eclipse 编写Java代码时,自动生成的注释信息都是按照预先设置好的格式生成的,例如其中author,datetime等属性值. 我们可以在Eclipse 中进行设置自己希望显示的注释信息 ...
- web前端设计:JQuery MINI UI
JQuery MINIUI 个人感觉用起来很爽,所以在此记录之,以后开发过程可能作为备选项.它能缩短开发时间,减少代码量,使开发者更专注于业务和服务端,轻松实现界面开发,带来绝佳的用户体验.在线下载地 ...
- UVA_埃及分数(Hard Version) UVA 12588
Problem EEg[y]ptian Fractions (HARD version)Given a fraction a/b, write it as a sum of different Egy ...
- 一张图讲解为什么需要自己搭建自己的git服务以及搭建的途径
图片信息量有点大.不废话上图 图中的一些链接: gitlab官方安装文档 https://github.com/gitlabhq/gitlabhq/blob/master/doc/install/in ...
- PHP MySQL Insert Into 之 Insert
向数据库表插入数据 INSERT INTO 语句用于向数据库表添加新记录. 语法 INSERT INTO table_name VALUES (value1, value2,....) 您还可以规定希 ...
- telnet查看memcached运行参数说明
在Linux/Windows系统中启动memcached的命令请查看http://weilingfeng98.iteye.com/admin/blogs/1741179 启动完memcached服务器 ...
- poj1080--Human Gene Functions(dp:LCS变形)
Human Gene Functions Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17206 Accepted: ...
- wpf动画概述
http://msdn.microsoft.com/zh-cn/library/vstudio/ms752312(v=vs.100).aspx Windows Presentation Foundat ...
- MFC多线程内存泄漏问题&解决方法
在用visual studio进行界面编程时(如MFC),前台UI我们能够通过MFC的消息循环机制实现.而对于后台的数据处理.我们可能会用到多线程来处理. 那么对于大多数人(尤其是我这样的菜鸟),一个 ...
- windows 下使clion支持c++11操作记录
最近用上了windows下的clion,发现默认安装的MINGW版本太低,导致所带的gcc版本竟然是3.5的,实在太老了,不支持c++11,于是手动修改了mingw的版本.首先去mingw的官网下载最 ...