Android Bundle傳遞資料

來源:互聯網
上載者:User

標籤:style   ring   讀取   tar   put   his   desc   傳遞資料   logs   

1.傳遞普通資料
                Intent intent=new Intent(MainActivity.this,TwoActivity.class);                Bundle bundle=new Bundle();                bundle.putString("name","張三");                bundle.putInt("age",18);                bundle.putString("gender","男");                intent.putExtras(bundle);                startActivity(intent);

擷取傳遞的資料

        Bundle bundle=getIntent().getExtras();        String name=bundle.getString("name");        String gender=bundle.getString("gender");        int age =bundle.getInt("age");    

 

2.傳遞Serializable資料

1.建立一個類實現Serializable

2.傳遞資料

                Intent intent=new Intent(MainActivity.this,TwoActivity.class);                Bundle bundle=new Bundle();                Person1 p1=new Person1("張三","男",18);                bundle.putSerializable("person",p1);                intent.putExtras(bundle);                startActivity(intent);

 

3.接受資料

        Bundle bundle=getIntent().getExtras();        Person1 p1= (Person1) bundle.getSerializable("person");        String name=p1.getName();        String gender=p1.getGender();        int age =p1.getAge();

 

 

 

3.傳遞Parcelable資料

1.建立類實現Parcelabel

public class Person3 implements Parcelable {    private String name;    private String gender;    private int age;    public Person3(String name, String gender, int age) {        this.name = name;        this.gender = gender;        this.age = age;    }    public String getName() {        return name;    }    public String getGender() {        return gender;    }    public int getAge() {        return age;    }    @Override    public String toString() {        return "Person3{" +                "name=‘" + name + ‘\‘‘ +                ", gender=‘" + gender + ‘\‘‘ +                ", age=" + age +                ‘}‘;    }    public static final Parcelable.Creator<Person3> CREATOR=new Parcelable.Creator<Person3>(){        /**         * 供外部類反序列話本類數組使用         * @param source         * @return         */        @Override        public Person3 createFromParcel(Parcel source) {            return new Person3(source);        }        /**         * 從Parcel中讀取資料         * @param size         * @return         */        @Override        public Person3[] newArray(int size) {            return new Person3[size];        }    };    /**     * 預設返回0就行     * @return     */    @Override    public int describeContents() {        return 0;    }    /**     * 把值寫進Parcel中     * @param dest     * @param flags     */    @Override    public void writeToParcel(Parcel dest, int flags) {        dest.writeString(name);        dest.writeString(gender);        dest.writeInt(age);    }    /**     * 這裡的讀取資料必須與writeToParacel(Parcel dest,int flags)一致,否則就會出錯     * @param source     */    public Person3(Parcel source) {        name = source.readString();        gender=source.readString();        age = source.readInt();    }}

 

2.傳遞資料

                Intent intent=new Intent(MainActivity.this,TwoActivity.class);                Bundle bundle=new Bundle();                Person3 p3=new Person3("張三","男",18);                bundle.putParcelable("person",p3);                intent.putExtras(bundle);                startActivity(intent);

 

3.接受資料

     Bundle bundle=getIntent().getExtras();        Person3 p3= bundle.getParcelable("person");        String name=p3.getName();        String gender=p3.getGender();        int age =p3.getAge();

 

Android Bundle傳遞資料

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.