Android項目實戰(十九):Android Studio 優秀外掛程式: Parcelable Code Generator

來源:互聯網
上載者:User

標籤:自己   現在   idt   sch   完成   重複   style   script   gen   

原文:Android項目實戰(十九):Android Studio 優秀外掛程式: Parcelable Code Generator

Android Studio 優秀外掛程式系列:

                      Android Studio 優秀外掛程式(一):GsonFormat

 

-----------------------------------------------------------------------------

Parcelable  , 這個詞大家應該不陌生吧,用於序列化對象的一個介面

不清楚的可以看一下這篇部落格:Intent傳遞對象的兩種方法

-----------------------------------------------------------------------------

這裡假設我們已經會使用 Parcelable 序列化一個對象了~~

那麼大家會發現 Parcelable 使用起來有些複雜,因為我們要自己複寫 幾個方法,而且當類的屬性比較多的時候,我們就會難受了,又要注意不寫錯屬性名稱,又要注意寫對屬性的類型,又要花不少的時間做重複的事情。

 

那麼因為 Parcelable 有使用它的優勢,我們又不能放棄,那該怎麼辦嗎?

Android Studio 提供給了我們 一個外掛程式用來簡化 給一個類 實現 Parcelable 介面的流程。

 

-----------------------------------------------------------------------------

現在學習下如何使用這個外掛程式:

 

1、Android Studio 開啟一個項目,點擊左上方 File -->Settings... 進行設定

 

 

 

 

2、選擇外掛程式Plugins , 搜尋Parcel,如果你沒有下載過這個外掛程式,那麼搜尋方塊下面會顯示“Nothing to show.Click Browse to....”

 

 

 

 

 

3、那就點擊藍色字型的 Browse 吧  ,這個時候會出現如的介面,我們只需要在左邊選中arcel然後點擊右面 綠色按鈕 "Install plugin" 就可以了

 

 

 

 

4、完成了上面三個步驟,就可以使用Parcelable Code Generator外掛程式了

怎麼用呢,

(1)建立一個類檔案,類名是看你需求自訂寫的,添加上你需要的屬性

(2)快速鍵 alt+insert ,會出現如下選擇框,選擇Parcelable 即可

 

然後我們就看到代碼,是不是比我們手動寫要快的許多

public class People implements Parcelable {    private int id;    private String url;    private int width;    private int height;    private int likeCount;    private String description;    private int time;    private int replyCount;    private int floorCount;    private int likeUserCount;    private int age;    private String name;    private String school;    private int type;    private String sax;    private int userid;    @Override    public int describeContents() {        return 0;    }    @Override    public void writeToParcel(Parcel dest, int flags) {        dest.writeInt(this.id);        dest.writeString(this.url);        dest.writeInt(this.width);        dest.writeInt(this.height);        dest.writeInt(this.likeCount);        dest.writeString(this.description);        dest.writeInt(this.time);        dest.writeInt(this.replyCount);        dest.writeInt(this.floorCount);        dest.writeInt(this.likeUserCount);        dest.writeInt(this.age);        dest.writeString(this.name);        dest.writeString(this.school);        dest.writeInt(this.type);        dest.writeString(this.sax);        dest.writeInt(this.userid);    }    public People() {    }    protected People(Parcel in) {        this.id = in.readInt();        this.url = in.readString();        this.width = in.readInt();        this.height = in.readInt();        this.likeCount = in.readInt();        this.description = in.readString();        this.time = in.readInt();        this.replyCount = in.readInt();        this.floorCount = in.readInt();        this.likeUserCount = in.readInt();        this.age = in.readInt();        this.name = in.readString();        this.school = in.readString();        this.type = in.readInt();        this.sax = in.readString();        this.userid = in.readInt();    }    public static final Parcelable.Creator<People> CREATOR = new Parcelable.Creator<People>() {        public People createFromParcel(Parcel source) {            return new People(source);        }        public People[] newArray(int size) {            return new People[size];        }    };}

 

Android項目實戰(十九):Android Studio 優秀外掛程式: Parcelable Code Generator

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.