Android项目实战(十九):Android Studio 优秀插件: Parcelable Code Generator

Android Studio 优秀插件系列:

                      Android Studio 优秀插件(一):GsonFormat

                      Android Studio 优秀插件(二): Parcelable Code Generator

 

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

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];
        }
    };
}

 

转载于:https://www.cnblogs.com/xqxacm/p/5242910.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Android Parcelable Code Generators是一种用于自动生成Android Parcelable接口相关代码的工具。在Android开发中,Parcelable接口常常用于在Activity之间传递自定义对象。但是手动实现Parcelable接口并编写相关的代码是繁琐且容易出错的,因此开发人员常常使用Parcelable Code Generators来自动生成这些代码。 Parcelable Code Generators可以帮助开发人员简化Parcelable接口的实现过程。它通过解析自定义对象的属性和方法,生成相应的Parcelable接口实现。开发人员只需要在自定义对象上添加一些注解或配置,然后使用Parcelable Code Generators生成器工具即可自动生成Parcelable相关的代码,包括序列化和反序列化的方法。 使用Parcelable Code Generators的好处是提高了开发效率,减少了手动实现Parcelable接口所需的工作量。开发人员只需要关注对象的定义和注解的配置,而无需费心编写复杂的序列化和反序列化代码。此外,Parcelable Code Generators还可以保证生成的代码的正确性和稳定性,避免了手动编写代码过程中可能出现的错误。 总之,Android Parcelable Code Generators是一种实用工具,能够帮助开发人员自动生成Parcelable相关代码,提高开发效率,减少错误。使用它可以简化开发过程,使得在Activity之间传递自定义对象变得更加方便和可靠。 ### 回答2: Android中实现Parcelable接口可以用于实现对象的序列化和反序列化,方便数据的传递和保存。然而,手动编写Parcelable的代码需要写入大量重复的代码,比较繁琐且易出错。因此,有一些代码生成器可以用来自动生成Parcelable代码,简化并加速开发过程。 一种常用的Android Parcelable代码生成器是插件"Parcelable Code Generator"。这个插件可以作为Android Studio的一个扩展来安装和使用。Parcelable Code Generator使用注解处理器,可以根据模板自动生成Parcelable相关代码。 使用这个插件,我们只需要在需要实现Parcelable的Java类上添加一个注解,例如"@Parcelable"。然后,编译项目时插件会自动扫描注解,根据类的字段自动生成Parcelable的代码。生成的代码中包含了Parcelable接口的实现和parcelable的函数,这样就可以将对象序列化到Parcel中,或者将Parcel中的数据还原到对象中。 Parcelable Code Generator还支持一些配置选项,比如可以指定Parcelable的名称、包名、是否实现Parcelable.Creator等。此外,插件还支持自定义Parcelable代码的模板,可以根据需求自定义生成的Parcelable代码的格式和内容。 总之,Android Parcelable Code Generator插件简化了Parcelable代码的编写,提高了开发效率和代码质量。使用这个插件,可以省去手动写入Parcelable的代码,减少了出错的可能性,节省了开发时间。它是Android开发中的一种常见工具,为我们提供了方便快捷的Parcelable代码生成功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值