Activity之间传递数据或数据包Bundle,传递对象,对象序列化,对象实现Parcelable接口
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.gaojinhua.android.activitymsg; 2 3 import android.content.Intent; 4 import android.os.Bundle; 5 import android.support.v7.app.AppCompatActivity; 6 import android.view.View; 7 import android.widget.EditText; 8 import android.widget.TextView; 9 import android.widget.Toast; 10 11 import java.util.regex.Pattern; 12 13 public class MainActivity extends AppCompatActivity { 14 public static final String DATA = "data"; 15 private EditText etName; 16 private EditText etAge; 17 private TextView textView; 18 private User user; 19 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_main); 24 25 etName = (EditText) findViewById(R.id.etName); 26 etAge = (EditText) findViewById(R.id.etAge); 27 textView = (TextView) findViewById(R.id.textView); 28 29 findViewById(R.id.btnStartSecond).setOnClickListener(new View.OnClickListener() { 30 @Override 31 public void onClick(View v) { 32 33 if (Pattern.compile("[a-zA-Z]+").matcher(etName.getText().toString()).matches() && 34 Pattern.compile("[0-9]+").matcher(etAge.getText().toString()).matches()) { 35 user = new User(etName.getText().toString(), Integer.parseInt(etAge.getText().toString())); 36 Bundle b = new Bundle(); 37 b.putParcelable(DATA, user); 38 Intent i = new Intent(MainActivity.this, SecondActivity.class); 39 i.putExtras(b); 40 startActivityForResult(i, 0); 41 } else { 42 Toast.makeText(getApplicationContext(), "请输入完整或正确的数据", Toast.LENGTH_SHORT).show(); 43 } 44 45 } 46 }); 47 } 48 49 @Override 50 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 51 super.onActivityResult(requestCode, resultCode, data); 52 textView.setText("另一个Activity返回的数据是:" + data.getExtras().getString(DATA)); 53 } 54 55 56 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.gaojinhua.android.activitymsg; 2 3 import android.content.Intent; 4 import android.os.Bundle; 5 import android.support.v7.app.AppCompatActivity; 6 import android.view.View; 7 import android.widget.TextView; 8 9 public class SecondActivity extends AppCompatActivity { 10 private TextView textViewAty; 11 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.activity_second); 16 17 Intent i = getIntent(); 18 Bundle b = i.getExtras(); 19 User user = b.getParcelable(MainActivity.DATA); 20 21 textViewAty = (TextView) findViewById(R.id.textViewAty); 22 textViewAty.setText(String.format("User info(name=%s,age=%d)", user.getName(), user.getAge())); 23 24 findViewById(R.id.btnBack).setOnClickListener(new View.OnClickListener() { 25 @Override 26 public void onClick(View v) { 27 Intent i = new Intent(); 28 Bundle b = new Bundle(); 29 b.putString(MainActivity.DATA, textViewAty.getText().toString()); 30 i.putExtras(b); 31 setResult(1, i); 32 finish(); 33 } 34 }); 35 } 36 37 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.gaojinhua.android.activitymsg; 2 3 import android.os.Parcel; 4 import android.os.Parcelable; 5 6 /** 7 * Created by Gaojinhua on 2015/8/14. 8 */ 9 public class User implements Parcelable { 10 public static final Creator<User> CREATOR = new Creator<User>() { 11 @Override 12 public User createFromParcel(Parcel source) { 13 return new User(source.readString(), source.readInt()); 14 } 15 16 @Override 17 public User[] newArray(int size) { 18 return new User[size]; 19 } 20 }; 21 private String name; 22 private int age; 23 24 25 public User(String name, int age) { 26 this.name = name; 27 this.age = age; 28 } 29 30 public String getName() { 31 return name; 32 } 33 34 public void setName(String name) { 35 this.name = name; 36 } 37 38 public int getAge() { 39 return age; 40 } 41 42 public void setAge(int age) { 43 this.age = age; 44 } 45 46 @Override 47 public int describeContents() { 48 return 0; 49 } 50 51 @Override 52 public void writeToParcel(Parcel dest, int flags) { 53 dest.writeString(getName()); 54 dest.writeInt(getAge()); 55 } 56 57 58 }
Activity之间传递数据或数据包Bundle,传递对象,对象序列化,对象实现Parcelable接口
加入了输入校验
防止接受空数据和类型不匹配的数据
git:https://github.com/gaojinhua/ActivityMsg.git