Activity之间传递数据或数据包Bundle,传递对象,对象序列化,对象实现Parcelable接口

 package com.gaojinhua.android.activitymsg;

 import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast; import java.util.regex.Pattern; public class MainActivity extends AppCompatActivity {
public static final String DATA = "data";
private EditText etName;
private EditText etAge;
private TextView textView;
private User user; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); etName = (EditText) findViewById(R.id.etName);
etAge = (EditText) findViewById(R.id.etAge);
textView = (TextView) findViewById(R.id.textView); findViewById(R.id.btnStartSecond).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { if (Pattern.compile("[a-zA-Z]+").matcher(etName.getText().toString()).matches() &&
Pattern.compile("[0-9]+").matcher(etAge.getText().toString()).matches()) {
user = new User(etName.getText().toString(), Integer.parseInt(etAge.getText().toString()));
Bundle b = new Bundle();
b.putParcelable(DATA, user);
Intent i = new Intent(MainActivity.this, SecondActivity.class);
i.putExtras(b);
startActivityForResult(i, 0);
} else {
Toast.makeText(getApplicationContext(), "请输入完整或正确的数据", Toast.LENGTH_SHORT).show();
} }
});
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
textView.setText("另一个Activity返回的数据是:" + data.getExtras().getString(DATA));
} }

MainActivity

 package com.gaojinhua.android.activitymsg;

 import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView; public class SecondActivity extends AppCompatActivity {
private TextView textViewAty; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second); Intent i = getIntent();
Bundle b = i.getExtras();
User user = b.getParcelable(MainActivity.DATA); textViewAty = (TextView) findViewById(R.id.textViewAty);
textViewAty.setText(String.format("User info(name=%s,age=%d)", user.getName(), user.getAge())); findViewById(R.id.btnBack).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent();
Bundle b = new Bundle();
b.putString(MainActivity.DATA, textViewAty.getText().toString());
i.putExtras(b);
setResult(1, i);
finish();
}
});
} }

SecondActivity

 package com.gaojinhua.android.activitymsg;

 import android.os.Parcel;
import android.os.Parcelable; /**
* Created by Gaojinhua on 2015/8/14.
*/
public class User implements Parcelable {
public static final Creator<User> CREATOR = new Creator<User>() {
@Override
public User createFromParcel(Parcel source) {
return new User(source.readString(), source.readInt());
} @Override
public User[] newArray(int size) {
return new User[size];
}
};
private String name;
private int age; public User(String name, int age) {
this.name = name;
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} @Override
public int describeContents() {
return 0;
} @Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(getName());
dest.writeInt(getAge());
} }

User

Activity之间传递数据或数据包Bundle,传递对象,对象序列化,对象实现Parcelable接口

加入了输入校验

防止接受空数据和类型不匹配的数据

https://github.com/gaojinhua/ActivityMsg/tree/master/app/src/main/java/com/gaojinhua/android/activitymsg

git:https://github.com/gaojinhua/ActivityMsg.git

上一篇:最新的QT git代码到code.qt.io/cgit,还有planet.qt.io有许多博客


下一篇:CI框架源码学习笔记3——Log.php