Android中数据的传递
Posted 醉清风
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android中数据的传递相关的知识,希望对你有一定的参考价值。
android中当两个Activity需要有信息交互的时候,可以使用Intent。具体来说:
发送单一类型数据:
发送方: String data = "Hello SecondActivity"; Intent intent = new Intent(FirstActivity.this, SecondActivity.class); intent.putExtra("extra_data", data); startActivity(intent); #在onClick中调用 接收方: Intent intent = getIntent(); String data = intent.getStringExtra("extra_data");#可以在onCreate中读取
除了putExtra("key", value)/getStringExtra("key")之外, 根据传递的value的类型还有很多对,比如:
putExtra("key", intvalue)/getintExtra("key") putExtra("key", floatvalue)/getfloatExtra("key") putExtra("key", doublevalue)/getDoubleExtra("key")
putExtra("key", bundlevalue)/getBundleExtra("key")
...
发送多种数据类型的组合:
也可以用Bundle把很多数据类型(包括嵌套Bundle)放到Bundle中一并发送:
//sender
Bundle b = new Bundle(); b.putInt("age", 2); b.putString("data", "hello"); intent.putExtras(b); startActivity(intent);
//receiver
Intent i = getIntent(); Bundle b = i.getExtras(); int age = b.getInt("age"); String data = b.getString("data");
发送类对象:
方法1:Serializable
Serializable是Java提供的序列化方法,优点是使用简单,一切由系统负责;缺点是速度稍慢。
public class User implements Serializable{ private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public User(String name, Integer age){ this.name = name; this.age = age; } }
//sender
intent.putExtra("user", (Serializable) new User("JM", 30));
//Receiver Intent i = getIntent(); User user = (User) i.getSerializableExtra("user"); textView.setText(String.format("name: %s, age: %s", user.getName(), user.getAge()));
方法2:Parcelable
public class User implements Parcelable{ private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public User(String name, Integer age){ this.name = name; this.age = age; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel parcel, int i) { //write all the fields that want to send parcel.writeString(getName()); parcel.writeInt(getAge()); } public static final Creator<User> CREATOR = new Creator<User>() { @Override public User createFromParcel(Parcel parcel) { return new User(parcel.readString(), parcel.readInt()); } @Override public User[] newArray(int i) { return new User[i]; } }; }
//Sender intent.putExtra("user", (Parcelable) new User("JM", 30)); startActivity(intent);
//Receiver User user = (User) i.getParcelableExtra("user"); textView.setText(String.format("name: %s, age: %s", user.getName(), user.getAge()));
如果在User中有多个同类型字段,比如String,可以用Bundle来实现:
@Override public void writeToParcel(Parcel parcel, int i) { //write all the fields that want to send // parcel.writeString(getName()); // parcel.writeInt(getAge()); Bundle b = new Bundle(); b.putString("name", getName()); b.putInt("age", getAge()); parcel.writeBundle(b); } public static final Creator<User> CREATOR = new Creator<User>() { @Override public User createFromParcel(Parcel parcel) { // return new User(parcel.readString(), parcel.readInt()); Bundle b = parcel.readBundle(); return new User(b.getString("name"), b.getInt("age")); } @Override public User[] newArray(int i) { return new User[i]; } };
从别的Activity得到返回结果:
启动端:
startActivityForResult(intent, 0);
0表示请求码,说明此次请求的意图是什么。
接收端:
Intent i = new Intent(); i.putExtra("data", editText.getText().toString()); setResult(RESULT_OK, i); finish();
以上是关于Android中数据的传递的主要内容,如果未能解决你的问题,请参考以下文章
如何使用java将数据从片段传递到android中的另一个片段?