码迷,mamicode.com
首页 > 移动开发 > 详细

Android高级技巧-intent传递对象

时间:2019-04-26 00:30:14      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:序列   重写   ext   技巧   set   void   parcel   you   姓名   

一、使用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());

}
}

 

Android高级技巧-intent传递对象

标签:序列   重写   ext   技巧   set   void   parcel   you   姓名   

原文地址:https://www.cnblogs.com/837634902why/p/10771871.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!