Android高级技巧-intent传递对象

一、使用intent传递对象

(1)使用serializable接口

persion类

package com.example.jixujinjie;

import java.io.Serializable;

/**
 * Created by 海绵宝宝 on 2019/4/25.
 */

public class Persion  implements Serializable{//intent不允许传递对象类所以我们把他序列化,继承Serializab
    private int age;
    private String name;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

MainActivity中使用Intent传值

public void onClick(View v) {
                Persion persion=new Persion();
                persion.setAge(18);
                persion.setName("王怀宇");
                Intent intent=new Intent(MainActivity.this,Main2Activity.class);
                intent.putExtra("persion_mes",persion);
                startActivity(intent);
            }

Mian2Activity中接受

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Persion persion=(Persion)getIntent().getSerializableExtra("persion_mes");
        TextView textView1=(TextView)findViewById(R.id.T1);
        TextView textView2=(TextView)findViewById(R.id.T2);
        textView1.setText("姓名:"+persion.getName());
        textView2.setText("年龄:"+persion.getAge());

    }

(2)使用parcelable方式

persion类

package com.example.jixujinjie;

import android.os.Parcel;
import android.os.Parcelable;

import java.io.Serializable;

/**
 * Created by 海绵宝宝 on 2019/4/25.
 */

public class Persion  implements Parcelable{//继承Parcelable
    private int age;
    private String name;
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    //writeToParcel和describeContents是继承Parcelabel的必须重写的函数
    @Override
    public int describeContents() {
        return 0;
    }
  //在writeToParcel将类的属性写入
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(age);
    }
    //创建一个Creator接口的实现,createFromParcel中将之前写入的取出,注意顺序和之前一致
    public static final Parcelable.Creator<Persion> CREATOR=new Parcelable.Creator<Persion>(){
        @Override
        public Persion createFromParcel(Parcel source) {
            Persion persion=new Persion();
            persion.name=source.readString();
            persion.age=source.readInt();
            return  persion;
        }

        @Override
        public Persion[] newArray(int size) {
            return new Persion[size];
        }
    };

}

Main2Activity.java

package com.example.jixujinjie;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class Main2Activity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//获取方式换成getParcelableExtra
Persion persion=(Persion)getIntent().getParcelableExtra("persion_mes");
TextView textView1=(TextView)findViewById(R.id.T1);
TextView textView2=(TextView)findViewById(R.id.T2);
textView1.setText("姓名:"+persion.getName());
textView2.setText("年龄:"+persion.getAge());

}
}

 

posted @ 2019-04-25 23:36  王怀宇  阅读(973)  评论(0编辑  收藏  举报