ButterKnife使用详解

1.在app目录的gradle中添加

compile 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'

2.(1)在Activity的onCreate方法中的setContentView()方法后面添加一句代码:

ButterKnife.bind(this);

(2)在fragment的onCreateView()方法中:添加代码:

View view = inflater.inflate(R.layout.perfect, null);
ButterKnife.bind(this, view);

3.然后就可以使用了:

@BindView(R.id.textView)
TextView textView;

4.其实Android studio还有一个插件“ButterKnife Zelezny“,能够自动添加注解,下面奉上使用方法:
首先,安装ButterKnife Zelezny插件

这里写图片描述

重启生效。
然后,把光标移动到布局文件处(R.layout.activity_main),这一步至关重要!!!如图所示
这里写图片描述

右键单击
这里写图片描述

选中Generate
然后
这里写图片描述

点击Generate ButterKnife Injections,就可以自动生成注解了!
附带ButterKnife的官方网站 http://jakewharton.github.io/butterknife/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。