好的插件能加快项目的开发速度,尤其是一些针对重复性的代码的插件,所以在这里向大家推荐2款不错的插件,如果以后发现新的好的插件,还会继续推荐,同时欢迎大家推荐
GsonFormat是一款将json直接转换成JavaBean的工具,这样就避免了我们经常需要照着接口文档来写实体类bean,而且还要看着不要写错,同时也节省了大量的时间
第一步:安装
首先点击设置按钮,通过File菜单进入设置也行

然后选择Plugins

在上面输入框输入GsonFormat或者gson都行

然后点击browse
选择GsonFormat

点击右边的install,然后就等待安装完成并且重启
第二步:使用
首先我们创建一个类(类名不限),然后在类中按下alt+insert 或者右键点击generate也行,选择选择gsonformat,

或者完全可以直接按快捷键alt+s会弹出一个框

,吧得到的json复制进输入框点击ok

在点击ok就能直接生成bean了
我是用以下json生成的bean
1 2 3 4 5 6 7
| { "people":[ {"firstName":"Brett","lastName":"McLaughlin","email":"aaaa"}, {"firstName":"Jason","lastName":"Hunter","email":"bbbb"}, {"firstName":"Elliotte","lastName":"Harold","email":"cccc"} ] }
|
JavaBean
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| package wang.raye.viewdemo.bean;
import java.util.List;
public class JsonBean {
private List<PeopleBean> people;
public List<PeopleBean> getPeople() { return people; }
public void setPeople(List<PeopleBean> people) { this.people = people; }
public static class PeopleBean { private String firstName; private String lastName; private String email;
public String getFirstName() { return firstName; }
public void setFirstName(String firstName) { this.firstName = firstName; }
public String getLastName() { return lastName; }
public void setLastName(String lastName) { this.lastName = lastName; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; } } }
|
怎么样很方便吧,下面介绍PreIOC
PreIOC
PreIOC是针对PreIOC框架的一个插件,PreIOC是一个预编译的注解框架,关于详细的PreIOC相关资料可以去主页查看git.oschina或者github
第一步:安装
同前面gsonFormat的安装差不多,不过搜索的时候需要搜索PreIOC,目前关于PreIOC的结果只有一个,所以可以选择安装
第二步:使用
使用PreIOC有个前提,就是项目必须要使用了PreIOC 1.0.6版本,之前的老版本不予支持,同时为了避免Bug建议切换到新版,使用PreIOC 1.0.6
1
| compile 'wang.raye.preioc:preioccore:1.0.6'
|
同时好一个布局,在需要在Activity、Fragment、Adapter需要使用的控件设置好id,然后在布局名称处右键点击generate

选择Generate PreIOC Injections

在弹出的框中判断是否有需要修改的属性名称和不要引用的id(去掉勾选),如果需要点击事件则勾选OnClick列的checkbox,如果是创建ViewHolder则选择ViewHolder(用于Adapter),点击confirm就能自动生成了,建议打开自动导入,这样Android Studio就会自动导入类中的引用和去掉没用的引用

以下是我的xml布局和生成的类文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="300dp" android:layout_height="match_parent" android:orientation="vertical">
<android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" app:cardBackgroundColor="#ffffff" android:layout_marginTop="10dp" app:cardElevation="3dp" app:cardMaxElevation="3dp">
<RelativeLayout android:layout_width="300dp" android:layout_height="match_parent">
<ImageView android:id="@+id/iv_head" android:layout_width="match_parent" android:layout_height="300dp" android:layout_margin="5dp" android:src="@mipmap/b" android:scaleType="fitXY"/>
<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignBottom="@id/iv_head" android:background="#30000000" android:padding="5dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp">
<TextView android:id="@+id/tv_age" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff"/>
<TextView android:id="@+id/tv_height" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:layout_toRightOf="@id/tv_age" android:textColor="#ffffff"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff" android:id="@+id/tv_info" android:layout_alignParentRight="true"/> </RelativeLayout>
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:maxLines="2" android:layout_below="@id/iv_head" android:layout_margin="10dp" android:textSize="22sp" android:minLines="2" android:ellipsize="end" android:id="@+id/tv_desc" android:textColor="#666666"/> </RelativeLayout>
</android.support.v7.widget.CardView>
<TextView android:layout_width="90dp" android:layout_height="36dp" android:layout_gravity="top|center_horizontal" android:background="@drawable/test_bg" android:gravity="center" android:textSize="22sp" android:elevation="6dp" android:id="@+id/tv_name" android:textColor="#333333"/> </FrameLayout>
|
类文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| package wang.raye.viewdemo.ui;
import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.widget.ImageView; import android.widget.TextView;
import wang.raye.preioc.PreIOC; import wang.raye.preioc.annotation.BindById; import wang.raye.preioc.annotation.OnClick; import wang.raye.viewdemo.R;
public class Test extends FragmentActivity {
@BindById(R.id.iv_head) ImageView ivHead; @BindById(R.id.tv_age) TextView tvAge; @BindById(R.id.tv_height) TextView tvHeight; @BindById(R.id.tv_info) TextView tvInfo; @BindById(R.id.tv_desc) TextView tvDesc; @BindById(R.id.tv_name) TextView tvName;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test); PreIOC.binder(this); }
@OnClick(R.id.iv_head) public void onClick() { } }
|
怎么样也很方便吧