ButterKnife使用小结
项目官网:http://jakewharton.github.io/butterknife/
Github主页:https://github.com/JakeWharton/butterknife
这个注解框架强大好用,国外牛逼博主 JakeWharton 杰作,下面 代码片段时最新的 7.01.jar,跟以前的使用变化很大。
直接下载jar包,引入项目就行,基本使用代码片如下:
package com.example.butterknife; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.ButterKnife;
import butterknife.Bind;
import butterknife.OnClick;
import butterknife.OnItemClick;
import butterknife.OnLongClick;
import java.util.List; import static android.widget.Toast.LENGTH_SHORT; public class SimpleActivity extends Activity { //一个可以实现全局View都调用的方法,需要用集合或者数组来传入View对象
private static final ButterKnife.Action<View> ALPHA_FADE = new ButterKnife.Action<View>() {
@Override public void apply(View view, int index) {
AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
alphaAnimation.setFillBefore(true);
alphaAnimation.setDuration(500);
alphaAnimation.setStartOffset(index * 100);
view.startAnimation(alphaAnimation);
}
};
//最基本的注解单个View对象
@Bind(R.id.title) TextView title;
@Bind(R.id.subtitle) TextView subtitle;
@Bind(R.id.hello) Button hello;
@Bind(R.id.list_of_things) ListView listOfThings;
@Bind(R.id.footer) TextView footer;
//扩展,可以注解多个View对象
@Bind({ R.id.title, R.id.subtitle, R.id.hello })
List<View> headerViews; private SimpleAdapter adapter;
//实现VIew的onclick方法,Id用onclick注解以后,后面直接方法名就可以使用,不需要再二次调用sayHello方法
@OnClick(R.id.hello) void sayHello() {
Toast.makeText(this, "Hello, views!", LENGTH_SHORT).show();
ButterKnife.apply(headerViews, ALPHA_FADE);
}
//同理onclick
@OnLongClick(R.id.hello) boolean sayGetOffMe() {
Toast.makeText(this, "Let go of me!", LENGTH_SHORT).show();
return true;
} @OnItemClick(R.id.list_of_things) void onItemClick(int position) {
Toast.makeText(this, "You clicked: " + adapter.getItem(position), LENGTH_SHORT).show();
} @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity);
//初始化Butter Knife
ButterKnife.bind(this); // Contrived code to use the bound fields.
title.setText("Butter Knife");
subtitle.setText("Field and method binding for Android views.");
footer.setText("by Jake Wharton");
hello.setText("Say Hello"); adapter = new SimpleAdapter(this);
listOfThings.setAdapter(adapter);
}
}
开发中最常用的适配器,用注解代码如下:
package com.example.butterknife; import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.Bind; public class SimpleAdapter extends BaseAdapter {
private static final String[] CONTENTS =
"The quick brown fox jumps over the lazy dog".split(" "); private final LayoutInflater inflater; public SimpleAdapter(Context context) {
inflater = LayoutInflater.from(context);
} @Override public int getCount() {
return CONTENTS.length;
} @Override public String getItem(int position) {
return CONTENTS[position];
} @Override public long getItemId(int position) {
return position;
} @Override public View getView(int position, View view, ViewGroup parent) {
ViewHolder holder;
if (view != null) {
holder = (ViewHolder) view.getTag();
} else {
view = inflater.inflate(R.layout.simple_list_item, parent, false);
holder = new ViewHolder(view);
view.setTag(holder);
} String word = getItem(position);
holder.word.setText("Word: " + word);
holder.length.setText("Length: " + word.length());
holder.position.setText("Position: " + position);
// Note: don't actually do string concatenation like this in an adapter's getView. return view;
} static class ViewHolder {
@Bind(R.id.word) TextView word;
@Bind(R.id.length) TextView length;
@Bind(R.id.position) TextView position; ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}
开发版本,没有正式发布的时候,可以再Application中设置:
package com.example.butterknife; import android.app.Application;
import butterknife.ButterKnife; public class SimpleApp extends Application {
@Override public void onCreate() {
super.onCreate();
ButterKnife.setDebug(BuildConfig.DEBUG);
}
}
ButterKnife使用小结的更多相关文章
- 【腾讯Bugly干货分享】深入理解 ButterKnife,让你的程序学会写代码
本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/578753c0c9da73584b025875 0.引子 话说我们做程序员的,都 ...
- Android注解使用之通过annotationProcessor注解生成代码实现自己的ButterKnife框架
前言: Annotation注解在Android的开发中的使用越来越普遍,例如EventBus.ButterKnife.Dagger2等,之前使用注解的时候需要利用反射机制势必影响到运行效率及性能,直 ...
- 从零开始编写自己的C#框架(26)——小结
一直想写个总结,不过实在太忙了,所以一直拖啊拖啊,拖到现在,不过也好,有了这段时间的沉淀,发现自己又有了小小的进步.哈哈...... 原想框架开发的相关开发步骤.文档.代码.功能.部署等都简单的讲过了 ...
- Python自然语言处理工具小结
Python自然语言处理工具小结 作者:白宁超 2016年11月21日21:45:26 目录 [Python NLP]干货!详述Python NLTK下如何使用stanford NLP工具包(1) [ ...
- java单向加密算法小结(2)--MD5哈希算法
上一篇文章整理了Base64算法的相关知识,严格来说,Base64只能算是一种编码方式而非加密算法,这一篇要说的MD5,其实也不算是加密算法,而是一种哈希算法,即将目标文本转化为固定长度,不可逆的字符 ...
- Android Butterknife 8.4.0 使用方法总结
转载请标明出处:http://www.cnblogs.com/zhaoyanjun/p/6016341.html 本文出自[赵彦军的博客] 前言 ButterKnife 简介 ButterKnife是 ...
- iOS--->微信支付小结
iOS--->微信支付小结 说起支付,除了支付宝支付之外,微信支付也是我们三方支付中最重要的方式之一,承接上面总结的支付宝,接下来把微信支付也总结了一下 ***那么首先还是由公司去创建并申请使用 ...
- Android开发学习之路-使用annotationProcessor配置Butterknife
Apt工具的作者宣布了不再维护该工具了,而且Android Studio也有了自己的插件,并且可以通过gradle来简单的配置. 其实用Butterknife的都知道,没有apt,onClick绑定不 ...
- iOS 之UITextFiled/UITextView小结
一:编辑被键盘遮挡的问题 参考自:http://blog.csdn.net/windkisshao/article/details/21398521 1.自定方法 ,用于移动视图 -(void)mov ...
随机推荐
- jquery 获取当前元素的索引值
$("#lisa > li").mouseover(function(){ alert($("#lisa > li").length); alert ...
- c++:类中的static成员
首先静态成员可以是public的,也可以是private的,只需在一般的变量.函数声明语句前加上static关键字即可声明一个static变量. 类中的静态成员存在与任何对象之外,所有该类对象的共享一 ...
- 用C++编写程序,输出两个字符串的最大公共子字符串
#include<iostream> #include<string> using namespace std; int main() { string s_l,s_sh; ...
- Linux学习之查看远端的端口是否通畅
以80端口为例解答 法一:此法常被用来检测是个远端端口是否通畅 [root@oldboy ~]# telnet baidu.com Trying 123.125.114.144... Connecte ...
- php Mysql 和Mysqli数据库函数整合
PHP Mysql和Mysqli数据库函数整合 服务器如果支持mysqli函数将优先mysqli函数进行数据库操作 否则将调用mysql函数进行数据库操作 用法SQL::connect(host,us ...
- 动态sql构建的过程
基本原理:使用xsqlbuilder框架完成动态sql的构建. 基本流程:使用WebUtils.getParametersStartingWith(ServletActionContext.getRe ...
- AutoCompleteTextview、MultiAutoCompleteTextView
AutoCompleteTextview 动态匹配输入内容文本框 属性: android:completionThreshold="2"; ...
- 修改host文件的P处理
notepad C:\WINDOWS\system32\drivers\etc\hosts 用文档创建hosts文件,添加上面代码.把文件后缀修改为 .bat 就不用每次很麻烦的查找host文件了.
- 安装GNUstep并运行第一个objc程序
在windows环境下安装GNUstep,运行objective-c程序,今天试了一下,记录一下操作步骤, 1,登陆http://ftpmain.gnustep.org/pub/gnustep/bin ...
- php的系统常量
认识一下系统常量 系统常量是PHP已经定义好的常量,我们可以直接拿来使用,常见的系统常量有: (1)__FILE__ :php程序文件名.它可以帮助我们获取当前文件在服务器的物理位置. (2)__LI ...