Intent传递数据的方法

使用intent的putExtra()方法,可以将要传递的数据附加到Intent对象,然后使用Intent对象进行数据传递。Intent中重载了很多putExtra()方法,可以附加的数据类型有:所有基本类型以及Serializable、Parcelable等类型的数据。

一、传递的数据是基本类型的

使用putExtra()方法将数据一条一条的附加到Intent对象中
MainActivity.java代码块:

Intent intent = new Intent(MainActivity.this, OtherAcitivity.class);
String name = "Jack";
int age = 18;
char sex = 'w';
intent.putExtra("myName",name);
intent.putExtra("myAge",age);
intent.putExtra("mySex",sex);
startActivity(intent);

OtherActivity.java代码块:

Intent intent = getIntent();
String name = intent.getStringExtra("myName");
int age = intent.getIntExtra("myAge",0);
char sex = intent.getCharExtra("mySex",'w');

System.out.println(name);
System.out.println(age);
System.out.println(sex);

二、传递的数据是对象

Android开发中,是不能将对象的引用直接传给Activities或者Fragments,需要先对象中的数据序列化转换成可以传输的二进制流后才能进行传输。这里使用Parcelable接口完成对象的序列化为例。
首先是Person.java的代码,Person实例化的对象就是我们要传递的数据。将Person实现Parcelable接口,重写writeToParcel()、describeContents()方法。

package cn.edu.jssvc.importexternmodule;
import android.os.Parcel;
import android.os.Parcelable;
public class Person implements Parcelable {
    private String name;
    private int age;
    private char sex;

    public Person(String name, int age, char sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    protected Person(Parcel in) {
        name = in.readString();
        age = in.readInt();
        sex = (char) in.readInt();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(age);
        dest.writeInt((int) sex);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<Person> CREATOR = new Creator<Person>() {
        @Override
        public Person createFromParcel(Parcel in) {
            return new Person(in);
        }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }
}

MainActivity.java代码块

Intent intent = new Intent(MainActivity.this, OtherAcitivity.class);
Person person = new Person("Bron",25, 'w');
Bundle bundle = new Bundle();
bundle.putParcelable("bundle",person);
intent.putExtra("intent",bundle);
startActivity(intent);

OtherActivity.java代码块

Intent intent = getIntent();
Bundle bundle = intent.getBundleExtra("intent");
Person person = bundle.getParcelable("bundle");
System.out.println(person.getName());
System.out.println(person.getAge());
System.out.println(person.getSex());
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值