MainActivity:
package com.tedu.parcelable;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class MainActivity extends Activity implements OnClickListener{
private Button btn;
private EditText etName;
private EditText etAge;
private RadioButton rgMale;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button) findViewById(R.id.button1);
etName=(EditText) findViewById(R.id.etName);
etAge=(EditText) findViewById(R.id.etAge);
rgMale=(RadioButton) findViewById(R.id.radio0);
btn.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
String username=etName.getText().toString().trim();
int age=Integer.parseInt(etAge.getText().toString());
String gender=rgMale.isChecked()?"男":"女";
Intent intent=new Intent(this,SecondActivity.class);
// intent.putExtra("_username",username);
// intent.putExtra("_age",age);
// intent.putExtra("_gender",gender);
//将数据封装到person对象
Person p=new Person();
p.setUsername(username);
p.setAge(age);
p.setGender(gender);
intent.putExtra("data",p);
startActivity(intent);
}
}
SecondActivity:
package com.tedu.parcelable;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class SecondActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Intent intent=getIntent();
// String username=intent.getStringExtra("_username");
// int age=intent.getIntExtra("_age",-1);
// String gender=intent.getStringExtra("_gender");
Person p=intent.getParcelableExtra("data");
Log.i("info","username --> "+p.getUsername());
Log.i("info","age --> "+p.getAge());
Log.i("info","gender --> "+p.getGender());
}
}
Person:
package com.tedu.parcelable;
import android.os.Parcel;
import android.os.Parcelable;
public class Person implements Parcelable{
private String username;
private int age;
private String gender;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Person() {
super();
}
public Person(String username, int age, String gender) {
super();
this.username = username;
this.age = age;
this.gender = gender;
}
public static final Parcelable.Creator<Person>
CREATOR=new Creator<Person>() {
@Override
public Person[] newArray(int size) {
// TODO Auto-generated method stub
return new Person[size];
}
@Override
public Person createFromParcel(Parcel source) {
Person p=new Person();
p.setUsername(source.readString());
p.setAge(source.readInt());
p.setGender(source.readString());
return p;
}
};
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(username);
dest.writeInt(age);
dest.writeString(gender);
}
}
activity_main:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="50dp"
android:text="姓名:" />
<EditText
android:id="@+id/etName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView1"
android:layout_alignRight="@+id/button1"
android:layout_toRightOf="@+id/textView1" >
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="36dp"
android:text="年龄:" />
<RadioGroup
android:id="@+id/rg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_centerVertical="true" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="男" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup>
<EditText
android:id="@+id/etAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView2"
android:layout_alignRight="@+id/etName"
android:layout_toRightOf="@+id/textView2" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/rg"
android:layout_alignLeft="@+id/rg"
android:text="性别:" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/rg"
android:layout_alignParentRight="true"
android:layout_below="@+id/rg"
android:layout_marginTop="41dp"
android:text="Button" />
</RelativeLayout>