Intent传递数据
http://blog.csdn.net/sukyle/article/details/4485505
创新互联-专业网站定制、快速模板网站建设、高性价比灞桥网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式灞桥网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖灞桥地区。费用合理售后完善,10多年实体公司更值得信赖。
1.Activity之间传递数据
(1)利用Intent传递数据
传递数据的Activity中:
Intent intent = new Intent();
intent.putExtra("name","Jon");//在Intent中加入键值对数据。键:name,值:Jon
intent.setClass(Activity01.this,Activity02.class);
Activity01.this.startActivity(intent);
在取出数据的Activity中:
Intent intent = getIntent();//获得传过来的Intent。
String value = intent.getStringExtra("name");//根据键name取出值。
(2)利用Bundle传递数据
传递数据的Activity:
Intent intent = new Intent();
Bundle myBundle = new Bundle();
myBundle.putString("Key_Name","Tom");
intent.putExtras(myBundle);
intent.setClass(Activity01.this,Activity02.class);
Activity01.this.startActivity(intent);
取出数据的Activity:
Bundle getBundle = getIntent().getExtras();
String value = getBundle.getString("Key_Name");
(3)利用startActivityForResult传递数据
startActivityForResult可以把数据传过去,还可以把那边的数据传过来。
传递数据的Activity中:
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("data", "somedata");//把数据传过去
intent.putExtras(bundle);
intent.setClass(Activity01.this, Activity02.class);
startActivityForResult(intent, 10);//10是一个代码
重载onActivityResult方法,用来接收传过来的数据:
protected void onActivityResult(int requestCode, int resultCode,Intent intent) {
switch (resultCode) {
case RESULT_OK:
Bundle b = intent.getExtras();
String str = b.getString("Result");
setTitle("Return data:" + str);
break;
default:
break;
}
}
接收数据的Activity:
Intent intent = getIntent();
Bundle getBundle = getIntent().getExtras();
String data = getBundle.getString("data");//读取传过来的数据
et.setText(data);
EditText edittext = (EditText) findViewById(R.id.text);
Intent intent = new Intent();//实例化一个Intent用来传过去,可以在Intent里存放数据。
Bundle bundle = new Bundle();
bundle.putString("Result",edittext.getText().toString());
intent.putExtras(bundle);
Activity02.this.setResult(RESULT_OK,intent);//把Intent(数据)传过去,RESULT_OK是请求码。
finish();//结束当前的Activity。
2.http://xqjay19910131-yahoo-cn.iteye.com/blog/1280857
Activity传递一个或者多个对象
Activity之间传递对象,或者通过Bundle传递对象的两种方式。
1:Serializable方式
传递一个对象
2:Parcelable方式
传递一个对象、传递多个对象(ArrayList
- public class CustomeClass implements Serializable{
- /**
- *
- */
- private static final long serialVersionUID = -7060210544600464481L;
- private String name;
- private String id;
- private int age;
- private String sex;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- 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 class CustomeClass implements Serializable{ /** * */ private static final long serialVersionUID = -7060210544600464481L; private String name; private String id; private int age; private String sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } 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; } }
发送部分:
- CustomeClass cc = new CustomeClass();
- cc.setAge(21);
- cc.setId("123456");
- cc.setName("mingkg21");
- cc.setSex("男");
- Intent intent = new Intent(this, PersonInfo.class);
- intent.putExtra("PERSON_INFO", cc);
- startActivity(intent);
CustomeClass cc = new CustomeClass(); cc.setAge(21); cc.setId("123456"); cc.setName("mingkg21"); cc.setSex("男"); Intent intent = new Intent(this, PersonInfo.class); intent.putExtra("PERSON_INFO", cc); startActivity(intent);
接收部分:
- Intent intent = getIntent();
- CustomeClass cc = CustomeClass)intent.getSerializableExtra("PERSON_INFO");
- setTextView(R.id.id, cc.getId());
- setTextView(R.id.name, cc.getName());
- setTextView(R.id.sex, cc.getSex());
- setTextView(R.id.age, String.valueOf(cc.getAge()));
Intent intent = getIntent(); CustomeClass cc = CustomeClass)intent.getSerializableExtra("PERSON_INFO"); setTextView(R.id.id, cc.getId()); setTextView(R.id.name, cc.getName()); setTextView(R.id.sex, cc.getSex()); setTextView(R.id.age, String.valueOf(cc.getAge()));
方式二:Parcelable
传递类:
- public class CustomeParcelable implements Parcelable {
- private String name;
- private String id;
- private int age;
- private String sex;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- 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 static final Parcelable.Creator
CREATOR = new Creator (){ - public CustomeParcelable createFromParcel(Parcel source) {
- // TODO Auto-generated method stub
- CustomeParcelable cus = new CustomeParcelable();
- cus.name = source.readString();
- cus.id = source.readString();
- cus.age = source.readInt();
- cus.sex = source.readString();
- return cus;
- }
- public CustomeParcelable[] newArray(int size) {
- // TODO Auto-generated method stub
- return new CustomeParcelable[size];
- }
- };
- public int describeContents() {
- // TODO Auto-generated method stub
- return 0;
- }
- public void writeToParcel(Parcel dest, int flags) {
- // TODO Auto-generated method stub
- dest.writeString(name);
- dest.writeString(id);
- dest.writeInt(age);
- dest.writeString(sex);
- }
- }
public class CustomeParcelable implements Parcelable { private String name; private String id; private int age; private String sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } 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 static final Parcelable.CreatorCREATOR = new Creator (){ public CustomeParcelable createFromParcel(Parcel source) { // TODO Auto-generated method stub CustomeParcelable cus = new CustomeParcelable(); cus.name = source.readString(); cus.id = source.readString(); cus.age = source.readInt(); cus.sex = source.readString(); return cus; } public CustomeParcelable[] newArray(int size) { // TODO Auto-generated method stub return new CustomeParcelable[size]; } }; public int describeContents() { // TODO Auto-generated method stub return 0; } public void writeToParcel(Parcel dest, int flags) { // TODO Auto-generated method stub dest.writeString(name); dest.writeString(id); dest.writeInt(age); dest.writeString(sex); } }
发送部分:
- CustomeParcelable cc = new CustomeParcelable();
- cc.setAge(21);
- cc.setId("123456");
- cc.setName("mingkg21");
- cc.setSex("男");
- Intent intent = new Intent(this, PersonInfo.class);
- intent.putExtra("PERSON_INFO", cc);
- startActivity(intent);
CustomeParcelable cc = new CustomeParcelable(); cc.setAge(21); cc.setId("123456"); cc.setName("mingkg21"); cc.setSex("男"); Intent intent = new Intent(this, PersonInfo.class); intent.putExtra("PERSON_INFO", cc); startActivity(intent);
接受部分:
- Intent intent = getIntent();
- CustomeParcelable cc = intent.getParcelableExtra("PERSON_INFO");
- setTextView(R.id.id, cc.getId());
- setTextView(R.id.name, cc.getName());
- setTextView(R.id.sex, cc.getSex());
- setTextView(R.id.age, String.valueOf(cc.getAge()));
Intent intent = getIntent(); CustomeParcelable cc = intent.getParcelableExtra("PERSON_INFO"); setTextView(R.id.id, cc.getId()); setTextView(R.id.name, cc.getName()); setTextView(R.id.sex, cc.getSex()); setTextView(R.id.age, String.valueOf(cc.getAge()));
以上为Parcelable传递一个对象,若要实现传递多个对象,
传递部分:
- Bundle bundle = new Bundle();
- bundle.putParcelableArrayList("mP3TagForNetDTOs",mP3TagForNetDTOs);
- msg.setData(bundle);
- endDocNotice.sendMessage(msg);
Bundle bundle = new Bundle(); bundle.putParcelableArrayList("mP3TagForNetDTOs",mP3TagForNetDTOs); msg.setData(bundle); endDocNotice.sendMessage(msg);
接受部分:
- Bundle bundle = msg.getData();
- mP3TagForNetDTOs = bundle.getParcelableArrayList("mP3TagForNetDTOs");
当前题目:Intent传递数据
当前路径:http://ybzwz.com/article/poocgp.html