Android快速开发(常用插件开发使用)
文/程序员男神
前言
新的一周开始了,上个周末同事都在加班整理需求、算权重,准备领今年的年终奖什么的。我们组只有我这个屌丝在家闲着,无事可做,那就撸代码吧!本来计划和几个要好的哥们出去打球,不幸的是腰又扭伤了。想到年会我还要表演街舞(年会本周五),心情有点低落啊。
扯得有点远了,言归正传。你还在为了很多个findViewById而奋笔疾书吗?你还在为了没什么技术含量却又经常写错的实体类而忧愁吗?你还在按钮点击效果绞尽脑汁的配置drawable吗?看完这篇文章,学会使用这些插件,你就可以轻轻松松完成上面复杂的工作,从此走上人生的巅峰!

一、ButterKnife插件的安装与使用
实现步骤
所有的插件安装都一样,首先我们打开File ---> Settings ---> Plugins界面搜索我们想要的插件,比如Android Butterknife Zelezny。
详细步骤如图:

安装插件--->Yes

安装......

重启Android Studio。

接下来就是如何使用这个插件:
首先我们添加ButterKnife的依赖,这里有两种方法:
第一种直接在gradle中添加依赖--->Sync Now

第二种方法,第四步之后搜索ButterKnife然后确定就OK了。

例如我们xml文件中有七个按钮:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.djj.viewpagerdome.activity.FirstActivity">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ViewPager基础用法" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ViewPager自主实现滑动指示条" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="带Tab的viewpager" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ViewPager+Fragment滑动实现" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TabLayout实现页卡" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ViewFlipper实现轮播图" />
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ViewPager实现轮播图" />
</LinearLayout>
返回activity找到布局引用处


设置好需要的监听--->confirm

效果图:

注意:
用了compile 'com.jakewharton:butterknife:8.4.0'这个版本之后,Android studio butterknife 注解成功但运行报空指针问题的解决?
第一步加入这三行代码,
apply plugin: 'com.neenbedankt.android-apt'
compile 'com.jakewharton:butterknife:8.0.1'
apt 'com.jakewharton:butterknife-compiler:8.0.1'
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.example.phc.recyclerveiwdemo"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:recyclerview-v7:23.3.0'
compile 'com.android.support:cardview-v7:23.3.0'
compile 'com.jakewharton:butterknife:8.0.1'
apt 'com.jakewharton:butterknife-compiler:8.0.1'
}
除此之外还需要 在整个工程的classpath中加入classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
更新:关于Android studio 3.0集成问题,更加简单。
参考文献:http://blog.csdn.net/pjingying/article/details/71975805?utm_source=itdadao&utm_medium=referral
简单点就是直接在module的gradle中添加以下依赖(其他地方不需要问):
compile 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
二、GsonFormat插件的安装与使用
实现步骤
插件的安装都一样这里不再阐述。
重要的是如何使用这个插件:
首先创建需要的实体类,Alt+Insert--->GsonFormat,把Json数据粘贴进去,点击OK。


效果如图,所有的字段都在这一个实体类里面,看着不方便,自己拆分开。让你不再担心写错字段给调试接口带来烦恼:

三、Android Selectors Generate插件的安装与使用
实现步骤
插件的安装都一样这里不再阐述。
重要的是如何使用这个插件:
首先我们要知道图片后缀必须为这样几种:


总结:
以上就是ButterKnife、GsonFormat、Android Selectors Generate插件的安装、使用以及需要注意的事项。