首页 > 代码库 > Android中数据的传递
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));
//ReceiverIntent 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中数据的传递
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。