ButterKnife 最新版本8.4.0详细使用
来源:互联网 发布:淘宝如何解除支付宝 编辑:程序博客网 时间:2024/05/16 18:48
相关链接:ButterKnife的github地址
ButterKnife简介:
1)这是一个注解框架,何为注解:
定义:注解(Annotation),也叫元数据。一种代码级别的说明。它是JDK1.5及以后版本引入的一个特性,与类、接口、枚举是在同一个层次。它可以声明在包、类、字段、方法、局部变量、方法参数等的前面,用来对这些元素进行说明,注释。
看不懂?没关系,讲下他的作用你就明白了。
平时编程时,写到大量的findViewById()和setOnClickListener(),而当你用到了ButterKnife
时,你就直接通过右键->Generate,就可以自动创建出来,从而可以节省时间,精简代码。
2)当前注解框架有很多,比如:AndroidAnnotations, XUtils, ButterKnife,
为啥选ButterKnife,这里我要引入别人的一张图片
上面的难易,强弱,快慢都是相对他们三个自己来说的,比如AndroidAnnotations的接入评级是难,并不代表它的接入方式很难,只是相对ButterKnife和XUtils来说比他们难。如果只想使用UI绑定,资源绑定,事件绑定的功能,推荐使用ButterKnife。
ButterKnife的详细使用(使用 Gradle 远程依赖):
1.导入依赖项:File->Project Structure->Dependencies->Library dependency
或者鼠标点下app再点右键->Open Module Settings->Dependencies->Library dependency
然后在输入框中输入ButterKnife,点击右边的搜索图标,就会出现下图。
选择com.jakewharton:butterknife:8.4.0,不要选错了,选错的话接下来的步奏会报错的。我就是掉进过这个坑
2.在项目的project 的build.gredle 文件中的dependencies标签下添加。(注意是project,不是app)
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
接下来进入到module的build.gredle里
3.在module的build.gredle 文件的顶部添加 apply plugin: 'android-apt'
,
在dependencies中添加 apt 'com.jakewharton:butterknife-compiler:8.4.0'
示例代码:
apply plugin: 'com.android.application'apply plugin: 'android-apt'android { compileSdkVersion 25 buildToolsVersion "24.0.3" defaultConfig { applicationId "com.lm.demo.recyclerview_demo" minSdkVersion 21 targetSdkVersion 25 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }}dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:25.0.1' compile 'com.jakewharton:butterknife:8.4.0' apt 'com.jakewharton:butterknife-compiler:8.4.0'}
4.点击右上角的 Sync Now,等下一下就行了
5.在Activity的onCreate()中添加ButterKnife.bind(this);
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); }}
需要注意的是ButterKnife.bind(this); 要在 setContentView(R.layout.activity_main);之后
6.选中我们的布局,右键,如果出现的是如下情况(没有Generate ButterKnife Injections选项),我们就还需要进行下一步
7.File->Settings->plugins->在输入框输入ButterKnife->点击下图蓝色字体 Browse
然后再点击 Install
这时会提示重启Android studio 。
8.然后再一次选中我们的布局,右键,就会出现下图的选项,点击Generate ButterKnife Injections
下图里面的Variable和OnClick,看一下出来的代码,想必你就明白什么意思了
public class MainActivity extends AppCompatActivity { @BindView(R.id.id_text) TextView idText; @BindView(R.id.id_text2) TextView text2; @BindView(R.id.id_imageView) ImageView idImageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); } @OnClick(R.id.id_text) public void onClick() { }}
感悟:在平时的一些demo中可以不用注解框架,但在一些完整的项目中,使用该框架会节省我们很多时间,代码看着也舒服。
另外,如果这里存在错误,还望各位指正,谢谢!
- ButterKnife 最新版本8.4.0详细使用
- 最新ButterKnife:8.4.0的使用
- 集成butterknife最新版本8.6.0
- Butterknife 8.4.0版本的使用:
- ButterKnife8.5.1最新版本使用详细步骤
- ButterKnife(8.4.0版本)原理分析
- Butterknife的详细初步使用
- ButterKnife:8.4.0的使用
- studio 3.0版本使用ButterKnife
- AndroidStudio中使用最新ButterKnife注解框架
- Android Butterknife 8.5.1 最新使用详解
- 【butterknife】butterknife:8.4.0 黄油刀使用详解。
- 关于ButterKnife 8.4.0以后版本报空指针的使用注意事项:
- NUnit2.0详细使用方法(最新版本2.4.6)
- cocopods最新版本使用
- AndroidStudio使用butterknife的详细介绍
- Android中ButterKnife的详细使用
- Android中ButterKnife的详细使用
- 微信公众平台开发—概述
- Python入门之print
- Python中pandas模块的使用方法
- ubuntu wireshark can't find netcard
- 我们该怎么样去定义分享经济
- ButterKnife 最新版本8.4.0详细使用
- JVM堆优化与相关配置参数
- Android笔记:给EditText设置边框
- 关于数据库设计时的自动增量字段的问题
- SpringMVC WEB项目 jsp引入的js,css文件找不到路径问题()
- nginx使用ssl模块配置HTTPS支持
- 返回上一页并刷新
- 回调函数的作用
- 第15周项目2-用哈希法组织关键字