Activity之间的参数传递
来源:互联网 发布:淘宝店用什么名字好听 编辑:程序博客网 时间:2024/05/02 04:41
参数传递主要是从一个Activity传递到另一个Activity,这里分三部分记载
第一部分是最简单的只有一个数据的时候的数据传递
//传递数据 Intent t = new Intent(MainActivity.this, AnotherActivity.class); t.putExtra("data", "hello another activity"); startActivity(t); //接收数据 如果不是String的就用 getIntExtra、getDoubleExtra、、、 String dataStr= getIntent().getStringExtra("data");第二部分是多个数据的时候的数据传递,这个时候使用Bundle装载数据
//传递数据 Intent t = new Intent(MainActivity.this, AnotherActivity.class); Bundle bundle = new Bundle(); bundle.putString("name", "zhaungsan"); bundle.putString("sex", "man"); bundle.putInt("age", 11); t.putExtras(bundle); startActivity(t); //接收数据 Bundle bundle=getIntent().getStringExtra("data"); String name= bundle.getString("name"); String sex= bundle.getString("sex"); Integer age= bundle.getInt("age");第三部份是传递自定义类型的数据
自定义数据类型的传递有两种:一种是实现Serializable接口;另一种是实现Parcelabe接口
实现Serializable接口
public class User implements Serializable { private String name; private String sex; private int age; public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public User(String _name, int _age) { this.age = _age; this.name = _name; } public User(String _name, int _age, String _sex) { this.age = _age; this.sex = _sex; this.name = _name; }}实现了Serializable接口后就可以直接用来传递数据了
//传递数据 Intent t = new Intent(MainActivity.this, AnotherActivity.class); t.putExtra("user", new User("zhangsan", 24,"man")); startActivity(t); //接收数据 User intUser = (User) getIntent().getSerializableExtra("user");
实现Parcelabe接口
public class User implements Parcelable { private String name; private String sex; private int age; public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public User(String _name, int _age) { this.age = _age; this.name = _name; } public User(String _name, int _age, String _sex) { this.age = _age; this.sex = _sex; this.name = _name; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { Bundle bundle = new Bundle(); bundle.putString("name", getName()); bundle.putString("sex", getSex()); bundle.putInt("age", getAge()); dest.writeBundle(bundle); } public static final Creator<User> CREATOR = new Creator<User>() { @Override public User createFromParcel(Parcel source) { Bundle bundle = source.readBundle(); return new User(bundle.getString("name", "zhangsan"), bundle.getInt("age", 0), bundle.getString("sex", "man")); } @Override public User[] newArray(int size) { return new User[size]; } };}实现了Parcelabe接口后就可以直接用来传递数据了
//传递数据 Intent t = new Intent(MainActivity.this, AnotherActivity.class); t.putExtra("user", new User("zhangsan", 24,"man")); startActivity(t); //接收数据 User intUser = (User) getIntent().getParcelableExtra("user");实现自定义数据传递的两种方法差别很大,一种使用简单,但是速度却很慢就是Serializable,一种使用有点麻烦,但是速度却很快就是Parcelabe。
0 0
- activity之间的参数传递
- Activity之间的参数传递
- activity之间参数传递
- activity之间参数传递
- activity之间参数传递
- activity之间参数传递
- Activity之间传递参数
- Activity之间传递参数
- Activity之间参数传递的方式
- android Activity之间的参数传递
- Activity之间参数传递的方式
- Activity之间传递参数的方法
- activity之间传递 intent 类型的参数
- android activity之间传递参数
- android activity之间传递参数
- android activity之间参数传递
- 两个Activity之间传递参数
- Android Activity之间参数传递
- 【读书笔记】JVM垃圾收集与内存分配策略
- 我会努力,不会轻言放弃
- IOS中.a库的封装和使用以及遇到i386的问题
- FlatBuffers文档-Building
- Android 之UI自适应解决方案
- Activity之间的参数传递
- 排序
- Android入门学习
- Oracle中Union,Union All,Intersect,Minus
- 再生核希尔伯特空间1---希尔伯特空间
- 预处理
- sql面试题
- undefined css/js/image file html文件
- oracle pl sql 配置问题