Butter Knife Android为控件设计的注解绑定库。

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

添加依赖:(具体看github官网)

dependencies {
implementation 'com.jakewharton:butterknife:9.0.0-rc2'
annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-rc2'
}

注意:·修饰类型不能是:private 或者 static;

   ButterKnife.bind(this);必须在setContentView();之后绑定;且父类bind绑定后,子类不需要再bind。

   在非Activity中,this不能替换成getActivity()。

   在Activity中不需要做解绑操作,在Fragment 中必须在onDestroyView()中做解绑操作。

·//绑定activity
ButterKnife.bind(this);

·//绑定fragment
ButterKnife.bind( this , view ) ;
·//解除绑定
ButterKnife.unbind(this);
·//多个控件id 注解
@BindViews({ R.id.button1 , R.id.button2 , R.id.button3 }public List<Button> buttonList ;

·//绑定string 字符串
@BindString( R.string.app_name )  public String str;
·//绑定string里面array数组
@BindArray(R.array.city );
·//绑定Bitmap 资源
@BindBitmap( R.mipmap.wifi )
·//绑定一个颜色值
@BindColor( R.color.colorAccent )
·//设置一个点击事件
@OnClick(R.id.button1 )
·//设置一个长按事件
@OnLongClick(R.id.button1)

在Adapter的ViewHolder中使用,将ViewHolder加一个构造方法,在new ViewHolder的时候把view传递进去。使用ButterKnife.bind(this, view)进行绑定,代码如下:

public class MyAdapter extends BaseAdapter {  

  @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.testlayout, parent, false);
holder = new ViewHolder(view);
view.setTag(holder);
} holder.name.setText("Donkor");
holder.job.setText("Android");
// etc...
return view;
} static class ViewHolder {
@BindView(R.id.title) TextView name;
@BindView(R.id.job) TextView job; public ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}

ButterKnife注解式绑定控件的更多相关文章

  1. Android Studio使用butterknife库绑定控件ID注解

    在线导入butterknife的jar包 在Android-app-Open Module Settings下选中module下的app 选择Dependencies,点击右边的“+”,选择第一个:1 ...

  2. asp.net学习之 数据绑定控件--表格绑定控件

    原文:asp.net学习之 数据绑定控件--表格绑定控件     数据绑定 Web 服务器控件是指可绑定到数据源控件,以实现在 Web 应用程序中轻松显示和修改数据的控件.数据绑定 Web 服务器控件 ...

  3. Dev控件GridView单元格绑定控件

    Dev控件GridView单元格绑定控件 //文本按钮 RepositoryItemButtonEdit btnFields = new RepositoryItemButtonEdit();//创建 ...

  4. asp.net 弹出式日历控件 选择日期 Calendar控件

    原文地址:asp.net 弹出式日历控件 选择日期 Calendar控件 作者:逸苡 html代码: <%@ Page Language="C#" CodeFile=&quo ...

  5. Kotlin 第一弹:自定义 ViewGroup 实现流式标签控件

    古人学问无遗力, 少壮工夫老始成.纸上得来终觉浅, 绝知此事要躬行. – 陆游 <冬夜读书示子聿> 上周 Google I/O 大会的召开,宣布了 Kotlin 语言正式成为了官方开发语言 ...

  6. android使用篇(四) 注解依赖注入IOC实现绑定控件

    在android使用篇(三) MVC模式中提到一个问题: 1) 视图层(View):一般採用XML文件进行界面的描写叙述,使用的时候能够很方便的引入,可是用xml编写了,又须要在Acitvity声明而 ...

  7. 你还在苦逼地findViewById吗?使用ButterKnife从此轻松定义控件

    前段时间笔者在苦逼地撸代码~最后发现有些复杂的界面在写了一屏幕的findviewbyid~~~另一堆setOnXXXListener~有没有方便一点的方法让我们简单点不用每次都定义一次.find一次, ...

  8. Android开发学习之路--Annotation注解简化view控件之初体验

    一般我们在写android Activity的时候总是会在onCreate方法中加上setContentView方法来加载layout,通过findViewById来实现控件的绑定,每次写这么多代码总 ...

  9. Dev控件VGridView单元格绑定控件

    实现的效果如下图: 1,实现分组显示 2,每行所绑定的控件不统一,内容自定义 实现方法: 采用VGridControl进行内容的定制 首先根据XML文件进行数据填充

随机推荐

  1. [Swift]LeetCode685. 冗余连接 II | Redundant Connection II

    In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) f ...

  2. [Swift]LeetCode686. 重复叠加字符串匹配 | Repeated String Match

    Given two strings A and B, find the minimum number of times A has to be repeated such that B is a su ...

  3. [Swift]LeetCode733. 图像渲染 | Flood Fill

    An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...

  4. js中对于逗号的运算符!

    先展示一个例子! var f = (function f() { return '1'; } , function g(){ return 1; } )(); console.log(typeof f ...

  5. windows服务器解决挖矿程序问题

    前几天发现服务器报警,cpu使用率已达100%,查资料知道正是最近比较流行的挖矿程序在捣鬼.我们使用的是阿里云的服务器,操作系统是windows server.网上有大量的资料讲如何处理,我把自己处理 ...

  6. Mysql篇--Linux中安装Mysql

    一.前述 由于Windows安装Mysql非常麻烦,所以分享一篇Linux中对MySQL的搭建,废话不多说,来,come on. 二.步骤 2.1 yum安装 yum install mysql-se ...

  7. 12.Flask-Restful

    定义Restful的视图 安装:pip install flask-restful 如果使用Flask-restful,那么定义视图函数的时候,就要继承flask_restful.Resourse类, ...

  8. C#版 - 226. Invert Binary Tree(剑指offer 面试题19) - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - 2 ...

  9. SpringBoot入门教程(十)应用监控Actuator

    Actuator可能大家非常熟悉,它是springboot提供对应用自身监控,以及对应用系统配置查看等功能.spring-boot-starter-actuator模块的实现对于实施微服务的中小团队来 ...

  10. hibernate易混淆点

    萌新小笔记: 用过hibernate的小伙伴肯定经常看到这几个东西吧!但是初学者常常会把这几个搞不清楚:dataSource,session,sessionFactory(dataSource),hi ...