Android中ButterKnife(黄油刀)的详细使用

article/2025/10/21 17:46:22

最近刚学会使用ButterKnife,真是超级好用,忍不住要分享给大家了。

写在前面:该文档使用7.0版本,8.0版本方法名有所改动,建议看官方文档,整体业务逻辑和原理没什么变动。

在android编程过程中,我们会写大量的布局和点击事件,像初始view、设置view监听这样简单而重复的操作让人觉得麻烦类,所以可以采用注解的方式去实现,而ButterKnife则是注解中相对简单易懂的很不错的开源框架,而网上的文档和例子都过时了,7.0之后的版本改动很大,之前的注解都不能用了,所以借鉴官方文档总结了一下,接下来就介绍一下如何使用。基本参照官方文档,加上自己的心得。


ButterKnife 优势:

1.强大的View绑定和Click事件处理功能,简化代码,提升开发效率

2.方便的处理Adapter里的ViewHolder绑定问题

3.运行时不会影响APP效率,使用配置方便

4.代码清晰,可读性强


使用心得:

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

2.Fragment ButterKnife.bind(this, mRootView);

3.属性布局不能用private or static 修饰,否则会报错

4.setContentView()不能通过注解实现。(其他的有些注解框架可以)


官网http://jakewharton.github.io/butterknife/

使用步骤:

一.导入ButterKnife jar包:

1)如果你是Eclipse,可以去官网下载jar包
2)如果你是AndroidStudio可以直接 File->Project Structure->Dependencies->Library dependency 搜索butterknife即可,第一个就是
3)当然也可以用maven和gradle配置

<span style="font-family:SimSun;font-size:14px;">MAVEN  <dependency>  <groupId>com.jakewharton</groupId>  <artifactId>butterknife</artifactId>  <version>(insert latest version)</version>  </dependency>  GRADLE  
compile 'com.jakewharton:butterknife:(insert latest version)'  Be sure to suppress this lint warning in your build.gradle.(关闭)  
lintOptions {  disable 'InvalidPackage'  
}  </span>

   


注意如果在Library 项目中使用要按如下步骤(github中有具体描述)否则无法找到view:


注:官网github也有对应的引用步骤。


二.常见使用方法:
1)由于每次都要在Activity中的onCreate绑定Activity,所以个人建议写一个BaseActivity完成绑定,子类继承即可
     注:ButterKnife.bind(this);绑定Activity 必须在setContentView之后:
     实现如下(FragmentActivity 实现一样):

<span style="font-family:SimSun;font-size:14px;">public abstract class BaseActivity extends Activity {  public abstract int getContentViewId();  @Override  protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(getContentViewId());  ButterKnife.bind(this);  initAllMembersView(savedInstanceState);  }  protected abstract void initAllMembersView(Bundle savedInstanceState);  @Override  protected void onDestroy() {  super.onDestroy();  ButterKnife.unbind(this);//解除绑定,官方文档只对fragment做了解绑  }  
}  </span>


2)绑定fragment
<span style="font-family:SimSun;font-size:14px;">public abstract class BaseFragment extends Fragment {  public abstract int getContentViewId();  protected Context context;  protected View mRootView;  @Nullable  @Override  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {  mRootView =inflater.inflate(getContentViewId(),container,false);  ButterKnife.bind(this,mRootView);//绑定framgent  this.context = getActivity();  initAllMembersView(savedInstanceState);  return mRootView;  }  protected abstract void initAllMembersView(Bundle savedInstanceState);  @Override  public void onDestroyView() {  super.onDestroyView();  ButterKnife.unbind(this);//解绑  }  
}  </span>

3)绑定view

<span style="font-family:SimSun;font-size:14px;">@Bind(R.id.hello_world)  
TextView mHelloWorldTextView;  
@Bind(R.id.app_name)  
TextView mAppNameTextView;//view</span>

4)绑定资源
<span style="font-family:SimSun;font-size:14px;">@BindString(R.string.app_name)  
String appName;//sting  
@BindColor(R.color.red)  
int textColor;//颜色  
@BindDrawable(R.mipmap.ic_launcher)  
Drawable drawable;//drawble  
@Bind(R.id.imageview)  
ImageView mImageView;  
@Bind(R.id.checkbox)  
CheckBox mCheckBox;  
@BindDrawable(R.drawable.selector_image)  
Drawable selector; </span>

5)Adapter ViewHolder 绑定

<span style="font-family:SimSun;font-size:14px;">public class TestAdapter extends BaseAdapter {  private List<String> list;  private Context context;  public TestAdapter(Context context, List<String> list) {  this.list = list;  this.context = context;  }  @Override  public int getCount() {  return list==null ? 0 : list.size();  }  @Override  public Object getItem(int position) {  return list.get(position);  }  @Override  public long getItemId(int position) {  return position;  }  @Override  public View getView(int position, View convertView, ViewGroup parent) {  ViewHolder holder;  if (convertView == null) {  convertView = LayoutInflater.from(context).inflate(R.layout.layout_list_item, null);  holder = new ViewHolder(convertView);  convertView.setTag(holder);  } else {  holder = (ViewHolder) convertView.getTag();  }  holder.textview.setText("item=====" + position);  return convertView;  }  static class ViewHolder {  @Bind(R.id.hello_world)  TextView textview;  public ViewHolder(View view) {  ButterKnife.bind(this, view);  }  }  
}  </span>

 

6)点击事件的绑定:不用声明view,不用setOnClickLisener()就可以绑定点击事件

a.直接绑定一个方法

<span style="font-family:SimSun;font-size:14px;">@OnClick(R.id.submit)  
public void submit(View view) {  // TODO submit data to server...  
} </span>

 

b.所有监听方法的参数是可选的

<span style="font-family:SimSun;font-size:14px;">@OnClick(R.id.submit)  
public void submit() {  // TODO submit data to server...  
}  </span>

c.定义一个特定类型,它将自动被转换

<span style="font-family:SimSun;font-size:14px;">@OnClick(R.id.submit)  
public void sayHi(Button button) {  button.setText("Hello!");  
}</span>

d.多个view统一处理同一个点击事件,很方便,避免抽方法重复调用的麻烦

<span style="font-family:SimSun;font-size:14px;">@OnClick({ R.id.door1, R.id.door2, R.id.door3 })  
public void pickDoor(DoorView door) {  if (door.hasPrizeBehind()) {  Toast.makeText(this, "You win!", LENGTH_SHORT).show();  } else {  Toast.makeText(this, "Try again", LENGTH_SHORT).show();  }  
} </span>

 e.自定义view可以绑定自己的监听,不指定id

<span style="font-family:SimSun;font-size:14px;">public class FancyButton extends Button {  @OnClick  public void onClick() {  // TODO do something!  }  
}  </span>

f.给EditText加addTextChangedListener(即添加多回调方法的监听的使用方法),利用指定回调,实现想回调的方法即可,哪个注解不会用点进去看下源码上的注释就会用了

<span style="font-family:SimSun;font-size:14px;">@OnTextChanged(value = R.id.mobileEditText, callback = OnTextChanged.Callback.BEFORE_TEXT_CHANGED)  
void beforeTextChanged(CharSequence s, int start, int count, int after) {  }  
@OnTextChanged(value = R.id.mobileEditText, callback = OnTextChanged.Callback.TEXT_CHANGED)  
void onTextChanged(CharSequence s, int start, int before, int count) {  }  
@OnTextChanged(value = R.id.mobileEditText, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)  
void afterTextChanged(Editable s) {  }  </span>


7)对一组View进行统一操作


a.装入一个list

<span style="font-family:SimSun;font-size:14px;">@Bind({ R.id.first_name, R.id.middle_name, R.id.last_name })  
List<EditText> nameViews; </span>

b.设置统一处理

<span style="font-family:SimSun;">static final ButterKnife.Action<View> DISABLE = new ButterKnife.Action<View>() {  @Override public void apply(View view, int index) {  view.setEnabled(false);  }  
};  
static final ButterKnife.Setter<View, Boolean> ENABLED = new ButterKnife.Setter<View, Boolean>() {  @Override public void set(View view, Boolean value, int index) {  view.setEnabled(value);  }  
};<span style="color: silver; line-height: normal; background-color: rgb(248, 248, 248);"> </span><a target=_blank href="http://blog.csdn.net/itjianghuxiaoxiong/article/details/50177549#" class="ViewSource" title="view plain" style="line-height: normal; color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url("images/default/ico_plain.gif"); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;">view plain</a></span><span data-mod="popu_168" style="font-size: 9px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; line-height: normal;"> <a target=_blank href="http://blog.csdn.net/itjianghuxiaoxiong/article/details/50177549#" class="CopyToClipboard" title="copy" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url("images/default/ico_copy.gif"); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: left top; background-repeat: no-repeat;">c</a></span>


c.统一操作处理,例如设置是否可点,属性等


<span style="font-family:SimSun;">ButterKnife.apply(nameViews, DISABLE);  
ButterKnife.apply(nameViews, ENABLED, false);</span><span style="font-family:Arial;font-size: 14px;"> </span>

8)可选绑定:默认情况下,“绑定”和“监听”绑定都是必需的。如果不能找到目标视图,则将抛出异常。所以做空处理


<span style="font-family:SimSun;">@Nullable @Bind(R.id.might_not_be_there) TextView mightNotBeThere;  @Nullable @OnClick(R.id.maybe_missing) void onMaybeMissingClicked() {  // TODO ...  
}  </span>


三、代码混淆

<span style="font-family:SimSun;font-size:14px;">-keep class butterknife.** { *; }  
-dontwarn butterknife.internal.**  
-keep class **$$ViewBinder { *; }  -keepclasseswithmembernames class * {  @butterknife.* <fields>;  
}  -keepclasseswithmembernames class * {  @butterknife.* <methods>;  
}  </span>

四、Zelezny插件的使用

在AndroidStudio->File->Settings->Plugins->搜索Zelezny下载添加就行 ,可以快速生成对应组件的实例对象,不用手动写。使用时,在要导入注解的Activity 或 Fragment 或 ViewHolder的layout资源代码上,右键——>Generate——Generate ButterKnife Injections,然后就出现如图的选择框。(此动态图来自官网)



原文链接: 点击打开链接

http://chatgpt.dhexx.cn/article/feaj5yAx.shtml

相关文章

黄油刀

【攻克Android (34)】Butter Knife 黄油刀 博客分类&#xff1a; 攻克Android系列 本文围绕以下四个部分展开&#xff1a; 一、注解式框架 二、Butter Knife 案例一 案例二&#xff1a;用 ListView 展示一个列表数据&#xff0c;每个Item里含有一个Button&#xff0c;可以…

ButterKnife黄油刀

ButterKnife黄油刀 1、强大的View绑定和Click事件处理功能&#xff0c;简化代码&#xff0c;提升开发效率 2、方便的处理Adapter里的ViewHolder绑定问题 3、运行时不会影响APP效率&#xff0c;使用配置方便 4、代码清晰&#xff0c;可读性强 怎么配置 在android Studio项…

Android版黄油刀简介

Butter Knife? 黄油刀是一个非常好的Android视图注入库。 黄油刀有助于减少许多样板代码&#xff08;例如&#xff0c;重复的findViewById调用&#xff09;。 如果您处理的活动具有大量的视图&#xff0c;那么您就会知道&#xff0c;将代码与“ findViewById”集群在一起时&a…

Android(ButterKnife)黄油刀使用详解

一、什么是ButterKnife黄油刀&#xff1f; 1.1ButterKnife中文又名黄油刀&#xff0c;是一款Android视图的字段和方法绑定快速注解框架。 1.2使用方法&#xff1a; 1.打开budild.gradle 文件 2.在dependencies 中添加 implementation com.jakewharton:butterknife:10.2.3// …

Android Butterknife(黄油刀) 使用方法总结

转载请标明出处&#xff1a;http://blog.csdn.net/donkor_/article/details/77879630 前言&#xff1a; ButterKnife是一个专注于Android系统的View注入框架,以前总是要写很多findViewById来找到View对象&#xff0c;有了ButterKnife可以很轻松的省去这些步骤。是大神JakeWha…

ArrayList$SubList.add导致的java.lang.StackOverflowError : null :慎用subList

项目场景&#xff1a; 上线后遇到的1个StackOverflowError问题&#xff0c;这里做个分析。通过日志文件可以看到&#xff1a; java.lang.StackOverflowError: nullat java.util.ArrayList$SubList.add(ArrayList.java:1047)at java.util.ArrayList$SubList.add(ArrayList.jav…

ArrayList和SubList的坑面试题

&#x1f468;&#x1f3fb;‍&#x1f393;博主介绍&#xff1a;大家好&#xff0c;我是芝士味的椒盐&#xff0c;一名在校大学生&#xff0c;热爱分享知识&#xff0c;很高兴在这里认识大家&#x1f31f; &#x1f308;擅长领域&#xff1a;Java、大数据、运维、电子 &#x…

NetSuite Sublist解释

今朝汇编一下Sublist主题的知识点以备忘。 2个数据源类型 Related Record - 以Saved Search建立的关联记录&#xff1b;Child Record - 父子表&#xff1b; 1. Related Record Saved Search关键点 这种形式的Sublist是利用Saved Search作为Sublist的数据源&#xff0c;将某…

各位,请慎用 subList!原来这么多坑!!

点击关注公众号&#xff0c;Java干货及时送达 1. 使用Arrays.asList的注意事项 1.1 可能会踩的坑 先来看下Arrays.asList的使用&#xff1a; List<Integer> statusList Arrays.asList(1, 2); System.out.println(statusList); System.out.println(statusList.contains(1…

Java中List的subList()方法及使用注意事项

List<Object> list new Arraylist<>();List<Object> subList list.subList(0, 5);其中subList(0, 5)取得的是下标为0到4的元素,不包含下标为5的元素. java.util.List中的subList方法返回列表中指定的 fromIndex&#xff08;包括 &#xff09;和 toIndex&a…

Java 中 List.subList() 方法的使用陷阱

转载请注明本文出自 clevergump 的博客&#xff1a;http://blog.csdn.net/clevergump/article/details/51105235, 谢谢! 前言 本文原先发表在我的 iteye博客: http://clevergump.iteye.com/admin/blogs/2211979, 但由于在 iteye发表的这篇文章的某些渲染曾经出现过一些问题, 我…

【Java】List的subList方法

Java的容器类ArrayList很常用&#xff0c;旗下存在一个subList方法&#xff0c;是值得注意的。 subList方法仅能够取出此ArrayList的引用&#xff0c;即使其看起来&#xff0c;好像是取出一个ArrayList的子ArrayList。 其实不然&#xff0c;subList方法的返回值&#xff0c;只是…

Java中的subList方法

Java中的subList方法 今天看到了java中List中有个subList的方法&#xff0c;感觉很熟悉有没有&#xff1f;没错&#xff0c;在Stirng类中&#xff0c;也有个类似的方法&#xff1a;subString。 Stirng中的subString方法&#xff0c;官方解释是&#xff1a;返回字符串的子字符串…

Java中List集合的subList方法

目录 一、说明 二、测试 1、直接输出 2、向subList中添加元素再输出 3、 从subList中删除元素再输出 4、向list中添加元素再输出 5、从list中删除一个元素后再输出 ​ 6、向list中添加元素&#xff0c;输出list&#xff0c;然后将subList传入ArrayList生成新集合在输出…

你真的会用ArrayList的subList方法吗?

导语 在日常的开发中通常会遇到截取List的情况&#xff0c;而大多数会选择使用subList方法进行截取&#xff0c;但是好多人对这个方法的理解都只是停留在使用层面上&#xff1f;这篇文章会非常详细达到源码级别的讲解sublList方法&#xff0c;需要的朋友赶紧收藏起来吧。 关于…

Java SubList 类 Java subList方法 Java ArrayList$SubList 方法特点 SubList 用法

Java SubList 类 Java subList方法 Java ArrayList$SubList 方法特点 SubList 用法 一、概述 在java集合中&#xff0c;常用ArrayList类中&#xff0c;若需要对 list进行截取&#xff0c;可以使用subList方法&#xff0c;进行生成 SubList的内部类&#xff0c;那么 ArrayList 和…

使用ArrayList中的subList方法

集合是Java开发日常开发中经常会使用到的。在之前的一些文章中&#xff0c;我们介绍过一些关于使用集合类应该注意的事项&#xff0c;如《为什么阿里巴巴禁止在 foreach 循环里进行元素的 remove/add 操作》、《为什么阿里巴巴建议集合初始化时&#xff0c;指定集合容量大小》等…

sublist详解

接口中定义 List<E> subList(int fromIndex, int toIndex);1&#xff0c;该方法返回的是父list的一个视图&#xff0c;从fromIndex&#xff08;包含&#xff09;&#xff0c;到toIndex&#xff08;不包含&#xff09;。fromIndextoIndex 表示子list为空 2&#xff0c;父…

数据建模应用

数据建模应用 一、为什么要数据建模二、数据建模种类1、关系建模&#xff08;3NF&#xff09;2、维度建模 三、3NF数据建模1、范式介绍2、3NF建模实战 四、维度建模1、维度和指标的概念2、星型模型3、雪花模型4、星型与雪花模型对比5、维度建模测试案例 五、3NF建模与维度建模的…

分享大数据建模工具-大数据挖掘建模平台

大数据挖掘建模平台 是面向企业级用户的大数据挖掘建模平台。平台采用可视化操作方式&#xff0c;通过丰富内置算法&#xff0c;帮助用户快速、一站式地进行数据分析及挖掘建模&#xff0c;可应用于处理海量数据、高复杂性的数据挖掘任务&#xff0c;为其提供准确、高精度的计算…