jar包下载:
http://download.csdn.net/detail/seven2729/9147765
点击事件没反应:
右键项目配置properties>>java compiler>>Annotation processing>>Factory Path,
勾选Enable project specific settings ,然后Add JARS,从项目libs文件夹下选择依赖包,点击完成。
-
在Activity里的写法
// 绑定View对象 @Bind(R.id.title) TextView title; @Bind(R.id.subtitle) TextView subtitle; @Bind(R.id.footer) TextView footer; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); // TODO 在这里直接用变量即可 }
在Fragment里的写法:
注意最好在onDestoryView中进行解绑
/**
* Fragment生命周期比较特殊
* 需要在onDestroyView及时释放这些View引用,避免造成内存溢出
*/
public class FancyFragment extends Fragment {
@Bind(R.id.button1)
Button button1;
@Bind(R.id.button2)
Button button2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fancy_fragment, container, false);
ButterKnife.bind(this, view); // 进行绑定
// TODO Use fields...
return view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
ButterKnife.unbind(this); // 解除绑定
}
}
- 在数据适配器的ViewHolder中的写法:
@Bind(R.id.title) TextView name;
@Bind(R.id.job_title) TextView jobTitle;
public ViewHolder(View view) {
ButterKnife.bind(this, view); // 进行绑定, 将view中的指定id对当前对象进行注入
}
- 依赖注入的其他写法:
// 也可以将这些View放到集合中
@Bind({ R.id.title, R.id.subtitle, R.id.footer })
List<TextView> textViews;
// 绑定字符串
@BindString(R.string.app_name) String titile;
// 绑定图片
@BindDrawable(R.drawable.ic_launcher) Drawable drawable;
// 绑定颜色值, 也可以是颜色选择器
@BindColor(R.color.colorPrimary) int colorPrimary; // int or ColorStateList field
// 绑定以像素为单位的尺寸或数值
@BindDimen(R.dimen.spacer) float spacer; // int (for pixel size) or float (for exact value) field
/**
* 给指定控件添加点击事件, 注意:参数View可以省略, 或者写成Button
* @param view
*/
@OnClick(R.id.bt_test)
public void submit(View view) {
// TODO submit data to server...
Toast.makeText(this, "single declare!", Toast.LENGTH_SHORT).show();
}
/**
* 给多个控件添加点击事件, 注意:参数View可以省略, 或者写成Button
* @param view
*/
@OnClick({R.id.bt_test, R.id.bt_test2})
public void submit2(View view) {
// TODO submit data to server...
switch (view.getId()) {
case R.id.bt_test:
Toast.makeText(this, "multi declare1!", Toast.LENGTH_SHORT).show();
break;
case R.id.bt_test2:
Toast.makeText(this, "multi declare2!", Toast.LENGTH_SHORT).show();
break;
}
}
/**
* 添加长按事件
* @return 表示是否消费长按事件 true消费
*/
@OnLongClick(R.id.bt_test) boolean sayGetOffMe() {
Toast.makeText(this, "Let go of me!", Toast.LENGTH_SHORT).show();
return true;
}
// 给ListView添加条目点击监听
@OnItemSelected(R.id.list_view)
void onItemSelected(int position) {
// TODO ...
}
5、添加混淆忽略:
-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$ViewBinder { *; }
-keepclasseswithmembernames class * {
@butterknife.* <fields>;
}
-keepclasseswithmembernames class * {
@butterknife.* <methods>;
}