http://blog.csdn.net/sukyle/article/details/4485505
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比灞橋網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式灞橋網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋灞橋地區(qū)。費(fèi)用合理售后完善,10多年實(shí)體公司更值得信賴。
1.Activity之間傳遞數(shù)據(jù)
(1)利用Intent傳遞數(shù)據(jù)
傳遞數(shù)據(jù)的Activity中:
Intent intent = new Intent();
intent.putExtra("name","Jon");//在Intent中加入鍵值對(duì)數(shù)據(jù)。鍵:name,值:Jon
intent.setClass(Activity01.this,Activity02.class);
Activity01.this.startActivity(intent);
在取出數(shù)據(jù)的Activity中:
Intent intent = getIntent();//獲得傳過(guò)來(lái)的Intent。
String value = intent.getStringExtra("name");//根據(jù)鍵name取出值。
(2)利用Bundle傳遞數(shù)據(jù)
傳遞數(shù)據(jù)的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);
取出數(shù)據(jù)的Activity:
Bundle getBundle = getIntent().getExtras();
String value = getBundle.getString("Key_Name");
(3)利用startActivityForResult傳遞數(shù)據(jù)
startActivityForResult可以把數(shù)據(jù)傳過(guò)去,還可以把那邊的數(shù)據(jù)傳過(guò)來(lái)。
傳遞數(shù)據(jù)的Activity中:
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("data", "somedata");//把數(shù)據(jù)傳過(guò)去
intent.putExtras(bundle);
intent.setClass(Activity01.this, Activity02.class);
startActivityForResult(intent, 10);//10是一個(gè)代碼
重載onActivityResult方法,用來(lái)接收傳過(guò)來(lái)的數(shù)據(jù):
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;
}
}
接收數(shù)據(jù)的Activity:
Intent intent = getIntent();
Bundle getBundle = getIntent().getExtras();
String data = getBundle.getString("data");//讀取傳過(guò)來(lái)的數(shù)據(jù)
et.setText(data);
EditText edittext = (EditText) findViewById(R.id.text);
Intent intent = new Intent();//實(shí)例化一個(gè)Intent用來(lái)傳過(guò)去,可以在Intent里存放數(shù)據(jù)。
Bundle bundle = new Bundle();
bundle.putString("Result",edittext.getText().toString());
intent.putExtras(bundle);
Activity02.this.setResult(RESULT_OK,intent);//把Intent(數(shù)據(jù))傳過(guò)去,RESULT_OK是請(qǐng)求碼。
finish();//結(jié)束當(dāng)前的Activity。
2.http://xqjay19910131-yahoo-cn.iteye.com/blog/1280857
Activity之間傳遞對(duì)象,或者通過(guò)Bundle傳遞對(duì)象的兩種方式。
1:Serializable方式
傳遞一個(gè)對(duì)象
2:Parcelable方式
傳遞一個(gè)對(duì)象、傳遞多個(gè)對(duì)象(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; } }
發(fā)送部分:
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()));
方式二: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.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); } }
發(fā)送部分:
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()));
以上為Parcelable傳遞一個(gè)對(duì)象,若要實(shí)現(xiàn)傳遞多個(gè)對(duì)象,
傳遞部分:
Bundle bundle = new Bundle(); bundle.putParcelableArrayList("mP3TagForNetDTOs",mP3TagForNetDTOs); msg.setData(bundle); endDocNotice.sendMessage(msg);
接受部分: