传递复杂对象(传递自定义对象)
XML部分 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="传递复杂对象"
android:id="@+id/button1"
/>
</RelativeLayout>
next.xml部分
<?xml version="1.0" encoding="utf-8"?>
<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"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/msg"
android:textSize="30dp"
/>
</RelativeLayout>
Activity部分
MainActivity.java 部分
package com.example.hx.myapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this,NextActivity.class);//创建Intent 意图
Person person=new Person();//实现NextActivity传递一个复杂对象
person.setAge(23);
person.setName("jajj");
intent.putExtra("msg",person);
startActivity(intent);
}
});
}
}
NextActivity.java 部分
package com.example.hx.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class NextActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
textView=(TextView)findViewById(R.id.msg);
Person person=(Person) getIntent().getParcelableExtra("msg");
textView.setText("-->>>"+person.toString());
}
}
Person.java 部分
package com.example.hx.myapplication;
import android.os.Parcel;
import android.os.Parcelable;
/**
* Created by hx on 2016/7/24.
*/
public class Person implements Parcelable{
private int age;
private String name;
@Override
public String toString() {
return "Person{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
private Person(Parcel parcel){
age =parcel.readInt();
name=parcel.readString();
}
public Person(){}
@Override
public int describeContents() {
return 0;
}
public static final Parcelable.Creator<Person> CREATOR= new Creator<Person>() {
@Override
public Person createFromParcel(Parcel parcel) {
return new Person(parcel);
}
@Override
public Person[] newArray(int i) {
return new Person[i];
}
};
@Override
public void writeToParcel(Parcel parcel, int i) {
//向pacel写入序列化得数据
parcel.writeInt(age);
parcel.writeString(name);
}
}