Android中butterknife的使用与自动化查找组件插件详解

时间:2019-10-08
本文章向大家介绍Android中butterknife的使用与自动化查找组件插件详解,主要包括Android中butterknife的使用与自动化查找组件插件详解使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

前言

Android开发中经常使用findViewById来获取控件然后进行一些列操作,当控件太多的时候代码就非常臃肿,今天就来学习一个新的开源库ButterKnife,真的可以帮助我们高效,快捷的开发,让我们的代码更加简洁。

首先我们来把ButterKnife集成在我们的项目中:ButterKnife的GitHub官方地址:github.com/JakeWharton

一、集成分为了两部分:

1.仅仅在App主工程使用:

在App的 build.gradle 中添加如下代码:

android {
...
// Butterknife requires Java 8.
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'com.jakewharton:butterknife:10.2.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.0'
}

然后将其应用到您的模块中:也在在App的 build.gradle中增加

apply plugin: 'com.android.library'//这是你自己的 检查下误复制
apply plugin: 'com.jakewharton.butterknife'

2.如果在Library projects中使用:

在Project的 build.gradle 中添加如下代码:

buildscript {
repositories {
mavenCentral()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.1'//这是你的gradle版本
classpath 'com.jakewharton:butterknife-gradle-plugin:10.2.0'
}
}

library中使用需要使用,如下:这就已经集成玩可以使用

class ExampleActivity extends Activity {
@BindView(R2.id.user)
EditText username;
@BindView(R2.id.pass)
EditText password;
...
}

二、如接下来介绍下的一个ButterKnife插件可以自动化查找组件并初始

1.在线引用

引用:Ctrl+Alt+S -> Plugins ->搜索 Android ButterKnife Zelezny -> Install plugin form disk ,从本地引入我们下载的jar包,添加成功后需要重启Android studio


2.使用

3.添加成功后,

把光标定位在activity_main的后面,注意是括号里边 前提是你在xml布局中命名好组件

setContentView(R.layout.activity_main);

右击选择Generate... 选择最后一行 或者使用快捷键Alt + Insert选择

低级Confirm就可以自动化生成代码了 前提是你在xml布局中命名好组件

总结

以上就是我在处理客户端真实IP的方法,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。