import java.io.Serializable;
public class SerData implements Serializable {
private static final long serialVersionUID = 999794470754667710L;
public int mAge;
public String mName;
public boolean mMarried;
public double mHeight;
}
import android.os.Parcel;
import android.os.Parcelable;
public class ParData implements Parcelable {
public int mAge;
public String mName;
public boolean mMarried;
public double mHeight;
// 写数据
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeInt(mAge);
out.writeString(mName);
out.writeByte((byte) (mMarried ? 1 : 0));
out.writeDouble(mHeight);
}
// 例行公事实现createFromParcel和newArray
public static final Parcelable.Creator CREATOR
= new Parcelable.Creator() {
// 读数据
public ParData createFromParcel(Parcel in) {
ParData par = new ParData();
par.mAge = in.readInt();
par.mName = in.readString();
par.mMarried = in.readByte() != 0;
par.mHeight = in.readDouble();
return par;
}
public ParData[] newArray(int size) {
return new ParData[size];
}
};
@Override
public int describeContents() {
return 0;
}
}
import com.example.exmparcelable.data.ParData;
import com.example.exmparcelable.data.SerData;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private EditText et_name, et_age, et_height;
private Spinner sp_married;
private String[] marriedStrArray = new String[]{"已婚", "未婚"};
private boolean[] marriedBoolArray = new boolean[]{true, false};
private boolean isMarried = true;
private void setMarriedSpinner(Context context, int spinner_id, int seq) {
sp_married = (Spinner) findViewById(spinner_id);
ArrayAdapter county_adapter;
county_adapter = new ArrayAdapter(context, R.layout.spinner_item, marriedStrArray);
county_adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
// setPrompt是设置弹出对话框的标题
sp_married.setPrompt("请选择婚否");
sp_married.setAdapter(county_adapter);
sp_married.setOnItemSelectedListener(new SpinnerSelectedListener());
if (seq >= 0) {
sp_married.setSelection(seq, true);
} else {
sp_married.setFocusable(false);
}
}
class SpinnerSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView> arg0, View arg1, int arg2, long arg3) {
isMarried = marriedBoolArray[arg2];
}
public void onNothingSelected(AdapterView> arg0) {
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_name = (EditText) findViewById(R.id.et_name);
et_age = (EditText) findViewById(R.id.et_age);
et_height = (EditText) findViewById(R.id.et_height);
setMarriedSpinner(this, R.id.sp_married, 0);
Button btn_ser = (Button) findViewById(R.id.btn_ser);
Button btn_par = (Button) findViewById(R.id.btn_par);
btn_ser.setOnClickListener(this);
btn_par.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (et_age.getText()==null || et_age.getText().length()<=0) {
Toast.makeText(this, "请输入年龄", Toast.LENGTH_LONG).show();
return;
} else if (et_name.getText()==null || et_name.getText().length()<=0) {
Toast.makeText(this, "请输入姓名", Toast.LENGTH_LONG).show();
return;
} else if (et_height.getText()==null || et_height.getText().length()<=0) {
Toast.makeText(this, "请输入身高", Toast.LENGTH_LONG).show();
return;
}
if (v.getId() == R.id.btn_ser) {
SerData ser = new SerData();
ser.mAge = Integer.parseInt(et_age.getText().toString());
ser.mName = et_name.getText().toString();
ser.mMarried = isMarried;
ser.mHeight = Double.parseDouble(et_height.getText().toString());
Intent intent = new Intent(this, SerializableActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("ser", ser);
intent.putExtras(bundle);
startActivity(intent);
} else if (v.getId() == R.id.btn_par) {
ParData par = new ParData();
par.mAge = Integer.parseInt(et_age.getText().toString());
par.mName = et_name.getText().toString();
par.mMarried = isMarried;
par.mHeight = Double.parseDouble(et_height.getText().toString());
Intent intent = new Intent(this, ParcelableActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("par", par);
intent.putExtras(bundle);
startActivity(intent);
}
}
}
import com.example.exmparcelable.data.SerData;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;
public class SerializableActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ser);
Bundle bundle = getIntent().getExtras();
SerData ser = (SerData) bundle.getSerializable("ser");
String desc = String.format("您输入的人物信息是:姓名%s,年龄%d,身高%f,婚否%b",
ser.mName, ser.mAge, ser.mHeight, ser.mMarried);
TextView tv_ser = (TextView) findViewById(R.id.tv_ser);
tv_ser.setText(desc);
}
}
import com.example.exmparcelable.data.ParData;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;
public class ParcelableActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_par);
Bundle bundle = getIntent().getExtras();
ParData par = bundle.getParcelable("par");
String desc = String.format("您输入的人物信息是:姓名%s,年龄%d,身高%f,婚否%b",
par.mName, par.mAge, par.mHeight, par.mMarried);
TextView tv_par = (TextView) findViewById(R.id.tv_par);
tv_par.setText(desc);
}
}
点击下载本文用到的对象序列化的工程代码
点此查看Android开发笔记的完整目录