Created
October 28, 2018 13:33
-
-
Save diaolizhi/5619cdfa42562cce2d6356e7015a72de to your computer and use it in GitHub Desktop.
Android Intent 传递对象
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //读 | |
| btn1.setOnClickListener(new View.OnClickListener() { | |
| @Override | |
| public void onClick(View v) { | |
| Intent intent = new Intent(MainActivity.this, WizardOne.class); | |
| intent.putExtra("setting", setting); | |
| startActivityForResult(intent, 1); | |
| } | |
| }); | |
| //取 | |
| final Intent intent = getIntent(); | |
| setting = intent.getParcelableExtra("setting"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.dlz.wizard; | |
| import android.os.Parcel; | |
| import android.os.Parcelable; | |
| public class Setting implements Parcelable { | |
| private int sex; | |
| private int age; | |
| private String name; | |
| public Setting() { | |
| } | |
| public Setting(int sex, int age, String name) { | |
| this.sex = sex; | |
| this.age = age; | |
| this.name = name; | |
| } | |
| /** | |
| * @Description: 自动生成的方法,通过 Parcel 对象创建 Setting 对象 | |
| * @Param: [in] | |
| * @return: | |
| * @Author: diaolizhi | |
| * @Date: | |
| */ | |
| protected Setting(Parcel in) { | |
| sex = in.readInt(); | |
| age = in.readInt(); | |
| name = in.readString(); | |
| } | |
| public static final Creator<Setting> CREATOR = new Creator<Setting>() { | |
| @Override | |
| public Setting createFromParcel(Parcel in) { | |
| return new Setting(in); | |
| } | |
| @Override | |
| public Setting[] newArray(int size) { | |
| return new Setting[size]; | |
| } | |
| }; | |
| public int getSex() { | |
| return sex; | |
| } | |
| public void setSex(int sex) { | |
| this.sex = sex; | |
| } | |
| 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; | |
| } | |
| @Override | |
| public int describeContents() { | |
| return 0; | |
| } | |
| @Override | |
| public void writeToParcel(Parcel dest, int flags) { | |
| dest.writeInt(sex); | |
| dest.writeInt(age); | |
| dest.writeString(name); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment