前言

  • ButterKnife 简介

    ButterKnife是一个专注于Android系统的View注入框架,可以减少大量的findViewById以及setOnClickListener代码,可视化一键生成。

项目github地址:https://github.com/JakeWharton/butterknife

  • ButterKnife 优势

    1、强大的View绑定和Click事件处理功能,简化代码,提升开发效率
    2、方便的处理Adapter里的ViewHolder绑定问题
    3、运行时不会影响APP效率,使用配置方便
    4、代码清晰,可读性强

如何添加依赖

  • 在项目的project 的build.gredle 文件中的dependencies标签下添加。

    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

例如:

buildscript {
repositories {
jcenter()
} dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
} allprojects {
repositories {
jcenter()
}
} task clean(type: Delete) {
delete rootProject.buildDir
}
  • 在module的build.gredle 文件中添加

    apply plugin: 'android-apt'

  • 在module的build.gredle 文件中的dependencies标签中添加

    compile 'com.jakewharton:butterknife:8.4.0'
    apt 'com.jakewharton:butterknife-compiler:8.4.0'

例如

apply plugin: 'com.android.application'
apply plugin: 'android-apt' android {
compileSdkVersion 24
buildToolsVersion "24.0.3" defaultConfig {
applicationId "com.zyj.wifi"
minSdkVersion 14
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
} dependencies {
compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.jakewharton:butterknife:8.4.0'
apt 'com.jakewharton:butterknife-compiler:8.4.0'
}

如何使用

  • 控件id 注解: @BindView()
package com.dl.ButterKnifeDemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button; import butterknife.BindView;
import butterknife.ButterKnife; public class ButterknifeActivity extends AppCompatActivity { @BindView( R.id.button1 )
public Button button1 ; // 注意:button 的修饰类型不能是:private 或者 static 。 否则会报错:错误: @BindView fields must not be private or static. (com.zyj.wifi.ButterknifeActivity.button1) @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_butterknife);
//绑定activity
ButterKnife.bind( this ) ; button1.setText( "I am a button ");
}
}
  • 多个控件id 注解: @BindViews()
package com.dl.ButterKnifeDemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import java.util.List;
import butterknife.BindViews;
import butterknife.ButterKnife; public class Main2Activity extends AppCompatActivity { @BindViews({ R.id.button1 , R.id.button2 , R.id.button3 })
public List<Button> buttonList ; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2); ButterKnife.bind(this); buttonList.get( 0 ).setText( "hello 1 ");
buttonList.get( 1 ).setText( "hello 2 ");
buttonList.get( 2 ).setText( "hello 3 ");
}
}
  • fragment 使用
package com.dl.ButterKnifeDemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button; import butterknife.BindView;
import butterknife.ButterKnife; public class ButterknifeFragment extends Fragment { @BindView( R.id.button1 )
public Button button1 ; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_butterknife, container, false); //绑定fragment
ButterKnife.bind( this , view ) ;
button1.setText( "I am a button ");
return view ;
}
}
  • @BindString() :绑定string 字符串
package com.dl.ButterKnifeDemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button; import butterknife.BindString;
import butterknife.BindView;
import butterknife.ButterKnife; public class ButterknifeActivity extends AppCompatActivity { @BindView( R.id.button1 ) //绑定button 控件
public Button button1 ; @BindString( R.string.app_name ) //绑定string 字符串
String meg; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_butterknife); //绑定activity
ButterKnife.bind( this ) ; button1.setText( meg );
}
}
  • @BindArray() : 绑定string里面array数组
<resources>
<string name="app_name">WiFi管家</string> <string-array name="city">
<item>厦门市</item>
<item>福州市</item>
<item>泉州市</item>
<item>漳州市</item>
<item>龙岩市</item>
</string-array> </resources> package com.dl.ButterKnifeDemo; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button; import butterknife.BindArray;
import butterknife.BindView;
import butterknife.ButterKnife; public class ButterknifeActivity extends AppCompatActivity { @BindView( R.id.button1 ) //绑定button 控件
public Button button1 ; @BindArray(R.array.city ) //绑定string里面array数组
String [] citys ; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_butterknife); //绑定activity
ButterKnife.bind( this ) ; button1.setText( citys[0] );
}
}
  • @BindBitmap( ) : 绑定Bitmap 资源
package com.dl.ButterKnifeDemo;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView; import butterknife.BindBitmap;
import butterknife.BindView;
import butterknife.ButterKnife; public class ButterknifeActivity extends AppCompatActivity { @BindView( R.id.imageView ) //绑定ImageView 控件
public ImageView imageView ; @BindBitmap( R.mipmap.wifi ) //绑定Bitmap 资源
public Bitmap wifi_bitmap ; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_butterknife); //绑定activity
ButterKnife.bind( this ) ; imageView.setImageBitmap( wifi_bitmap );
}
}
  • @BindColor( ) : 绑定一个颜色值
package com.dl.ButterKnifeDemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button; import butterknife.BindColor;
import butterknife.BindView;
import butterknife.ButterKnife; public class ButterknifeActivity extends AppCompatActivity { @BindView( R.id.button1 ) //绑定一个控件
public Button button1 ; @BindColor( R.color.colorAccent ) int black ; //绑定一个颜色值 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_butterknife); //绑定activity
ButterKnife.bind( this ) ; button1.setTextColor( black ); }
}
  • @OnClick( ) : 绑定控件点击事件
  • @OnLongClick( ) : 绑定控件长按事件
package com.dl.ButterKnifeDemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast; import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnLongClick; public class ButterknifeActivity extends AppCompatActivity { @OnClick(R.id.button1 ) //给 button1 设置一个点击事件
public void showToast(){
Toast.makeText(this, "is a click", Toast.LENGTH_SHORT).show();
} @OnLongClick( R.id.button1 ) //给 button1 设置一个长按事件
public boolean showToast2(){
Toast.makeText(this, "is a long click", Toast.LENGTH_SHORT).show();
return true ;
} @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_butterknife); //绑定activity
ButterKnife.bind( this ) ; }
}
  • zelezny : Butterknife插件的使用

    插件的安装

插件的使用

安装完成插件后,会提示重启AS,重启完后,可以写一个布局并且新建一个代码类测试下。测试的过程中要注意的是,需要将光标移到setContentView(R.layout.acty_login),将光标放到R.layout.acty_login,然后右键Generate就有了。要注意一定要将光标放在R.layout.acty_login上面。

这里需要注意的是在勾选控件的界面上,有一个CreateViewHolder , 很明显这个是专门为ListView或者RecyclerView的适配器专门提供的。

笔  者:MysticCoder
关于作者:专注于移动端开发。如有问题或建议,请多多赐教!
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
特此声明:所有评论和私信都会在第一时间回复。也欢迎园子的大大们指正错误,共同进步。或者直接私信
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是作者坚持原创和持续写作的最大动力!

Android注解利器:ButterKnife 的基本使用的更多相关文章

  1. Android 注解工具 ButterKnife

    Butter Knife 是 Android 视图字段和方法绑定,使用注解处理来生成样板代码. 主要特性: 在字段使用 @FindView消除findViewById调用 使用 @FindViews在 ...

  2. Android注解神器 ButterKnife框架

    前言: 本人是一个只有几个月工作经验的码小渣.这是我写的第一篇博客,如有不足之处还请大家不要介意,还请大佬可以指出问题. 在这几个月的实战开发中自己也遇到了很多问题,真的是举步艰难啊!!! 在实战开发 ...

  3. Android注解使用之通过annotationProcessor注解生成代码实现自己的ButterKnife框架

    前言: Annotation注解在Android的开发中的使用越来越普遍,例如EventBus.ButterKnife.Dagger2等,之前使用注解的时候需要利用反射机制势必影响到运行效率及性能,直 ...

  4. Android注解使用之ButterKnife 8.0注解使用介绍

    前言: App项目开发大部分时候还是以UI页面为主,这时我们需要调用大量的findViewById以及setOnClickListener等代码,控件的少的时候我们还能接受,控件多起来有时候就会有一种 ...

  5. Android学习笔记- ButterKnife 8.0注解使用介绍

    前言: App项目开发大部分时候还是以UI页面为主,这时我们需要调用大量的findViewById以及setOnClickListener等代码,控件的少的时候我们还能接受,控件多起来有时候就会有一种 ...

  6. Android注解框架实战-ButterKnife

    文章大纲 Android注解框架介绍 ButterKnife实战 项目源码下载   一.框架介绍 为什么要用注解框架?  在Android开发过程中,我们经常性地需要操作组件,操作方法有findVie ...

  7. Java Android 注解(Annotation) 及几个常用开源项目注解原理简析

    不少开源库(ButterKnife.Retrofit.ActiveAndroid等等)都用到了注解的方式来简化代码提高开发效率. 本文简单介绍下 Annotation 示例.概念及作用.分类.自定义. ...

  8. 开发自己的山寨Android注解框架

    目录 开发自己的山寨Android注解框架 开发自己的山寨Android注解框架 参考 Github黄油刀 Overview 在上一章我们学习了Java的注解(Annotation),但是我想大家可能 ...

  9. Android注解使用之使用Support Annotations注解优化代码

    前言: 前面学习总结了Java注解的使用,博客地址详见Java学习之注解Annotation实现原理,从本质上了解到什么注解,以及注解怎么使用?不要看见使用注解就想到反射会影响性能之类,今天我们就来学 ...

随机推荐

  1. 【转】 C++库常用函数一览

    本文中提到的函数库有:<string> <cctype> <algorithm> <cmath> <cstdlib> <iomanip ...

  2. office2010怎么激活

    软件都是不断更新换代的,像我们使用最多的Microsoft Office软件,从最初的98,2000,2003,2007,到现在的2010.但是在最初安装Office软件时,都是未激活的.下面介绍的就 ...

  3. 【POJ1151】【扫描线+线段树】Atlantis

    Description There are several ancient Greek texts that contain descriptions of the fabled island Atl ...

  4. PAT - 基础 - 最大公约数和最小公倍数

    题目: 本题要求两个给定正整数的最大公约数和最小公倍数. 输入格式: 输入在一行中给出2个正整数M和N(<=1000). 输出格式: 在一行中顺序输出M和N的最大公约数和最小公倍数,两数字间以1 ...

  5. Linux删除用户

    删除用户 # userdel abc 该删除操作将用户删除但保留用户的home文件夹和邮件文件夹.并且当用户abc正在登录的时候,删除操作将失败,如下: # userdel abc userdel: ...

  6. SVN 使用的简单整理

    1. 在SVN服务器上创建存储Dir,并和个人主机建立联系.      现在SVN服务器上创建一个存储文件夹svn_storeDir.然后在个人电脑上建立一个本地文件夹local_Dir.    进入 ...

  7. 关于Verilog 中的for语句的探讨

    在C语言中,经常用到for循环语句,但在硬件描述语言中for语句的使用较C语言等软件描述语言有较大的区别. 在Verilog中除了在Testbench(仿真测试激励)中使用for循环语句外,在Test ...

  8. H3C S5500上层接路由,VLAN IP作网站配置实例

    # version 5.20, Release 2208 # sysname S5500-1 # clock timezone #Web#8#01 add 08:00:00 # super passw ...

  9. Hibernate如何一个类映射两个表

    一个User类有username,password属性,还有 otherInformation等其他属性,username和password映射到一个表,otherInformation等其他属性映射 ...

  10. keil 51启动代码

    Startup code:启动代码. 在Keil中,启动代码在复位目标系统后立即被执行.启动代码主要实现以下功能: (1) 清除内部数据存储器 (2) 清除外部数据存储器 (3) 清除外部页存储器 ( ...