赠送源码:https://github.com/yugu88/MagicWX。

《最完整的Android逆向知识体系》

集成分为了两部分:

①仅仅在App主工程使用:

在App的 build.gradle 中添加如下代码:版本号9之前的没有适配AndroidX,工程迁移AndroidX时会报错。

dependencies {implementation 'com.jakewharton:butterknife:9.0.0-rc3'// 必须要有,不然无法绑定点击事件annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-rc3'
}

②如果在Library projects中使用:

在Project的 build.gradle 中添加如下代码:

buildscript {repositories {mavenCentral()}dependencies {classpath 'com.jakewharton:butterknife-gradle-plugin:9.0.0-rc3'}
}

library的build.gradle中添加如下:

apply plugin: 'com.android.library'
apply plugin: 'com.jakewharton.butterknife'...dependencies {implementation 'com.jakewharton:butterknife:9.0.0-rc3'// 必须要有,不然无法绑定点击事件annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-rc3'
}

library中使用需要使用R2,如下:

class MainActivity extends Activity {@BindView(R2.id.user) EditText username;@BindView(R2.id.pass) EditText password;}

ButterKnife 注意事项:

  • 在Activity 类中绑定 :ButterKnife.bind(this);必须在setContentView();之后绑定;且父类bind绑定后,子类不需要再bind。
  • 在非Activity 类(eg:Fragment、ViewHold)中绑定: ButterKnife.bind(this,view);这里的this不能替换成getActivity()。
  • 在Activity中不需要做解绑操作,在Fragment 中必须在onDestroyView()中做解绑操作。
  • 使用ButterKnife修饰的方法和控件,不能用private or static 修饰,否则会报错。错误: @BindView fields must not be private or static. (com.zyj.wifi.ButterknifeActivity.button1)
  • setContentView()不能通过注解实现。(其他的有些注解框架可以)
  • 使用Activity为根视图绑定任意对象时,如果你使用类似MVC的设计模式你可以在Activity 调用ButterKnife.bind(this, activity),来绑定Controller。
  • 使用ButterKnife.bind(this,view)绑定一个view的子节点字段。如果你在子View的布局里或者自定义view的构造方法里 使用了inflate,你可以立刻调用此方法。或者,从XML inflate来的自定义view类型可以在onFinishInflate回调方法中使用它。

在Activity中绑定ButterKnife:

public class MainActivity extends AppCompatActivity{ @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //绑定初始化ButterKnife 必须在setContentView之后调用ButterKnife.bind(this); }
}

在Fragment中绑定ButterKnife:

public class ButterknifeFragment extends Fragment{ private Unbinder unbinder; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment, container, false); //返回一个Unbinder值(进行解绑),注意这里的this不能使用getActivity() unbinder = ButterKnife.bind(this, view); return view;} /** * onDestroyView中进行解绑操作 */ @Override public void onDestroyView() { super.onDestroyView(); unbinder.unbind(); } }

在Adapter中绑定ButterKnife:

将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);// 将ViewHolder加一个构造方法,在new ViewHolder的时候把view传递进去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.bind(this, view); } }
}

ButterKnife的基本使用

/**
* 主App工程使用如下
*/
@BindView(R.id.txt_tips)
TextView mTxtTips;/**
* library工程使用如下
*/
@BindView( R2.id.button)
Button button; // 就是R和R2的区别,因为library中目前不能调用R文件了。// 同时绑定多个
@BindViews({ R2.id.button1, R2.id.button2, R2.id.button3})
public List<Button> buttonList ; //绑定资源文件中string字符串
@BindString(R2.string.app_name)  String str;  //绑定资源文件中string里面array数组
@BindArray(R2.array.city)  String [] citys ;  //绑定Bitmap 资源
@BindBitmap( R2.mipmap.bm)public Bitmap bitmap ; //具体色值在color文件中
@BindColor( R2.color.colorAccent ) int black; //绑定一个颜色值 @Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);ButterKnife.bind(this); buttonList.get( 0 ).setText( "hello 1 "); buttonList.get( 1 ).setText( "hello 2 "); buttonList.get( 2 ).setText( "hello 3 "); button.setText( str );button.setText(citys[0]);imageView.setImageBitmap(bitmap);button.setTextColor(black);  }@OnClick(R2.id.button1 ) //给 button1 设置一个点击事件
public void showToast(){ Toast.makeText(this, "is a click", Toast.LENGTH_SHORT).show();
}
@OnLongClick( R2.id.button1 ) //给 button1 设置一个长按事件
public boolean showToast2(){ Toast.makeText(this, "is a long click", Toast.LENGTH_SHORT).show();return true ;
}// 我们可以使用 Android studio 的 Butterknife 插件zelezny 快速生成@OnClick({R.id.ll_product_name, R.id.ll_product_lilv, R.id.ll_product_qixian, R.id.ll_product_repayment_methods})
public void onViewClicked(View view) {switch (view.getId()) { case R.id.ll_product_name: System.out.print("我是点击事件1"); break;case R.id.ll_product_lilv: System.out.print("我是点击事件2"); break; case R.id.ll_product_qixian: System.out.print("我是点击事件3"); break; case R.id.ll_product_repayment_methods: System.out.print("我是点击事件4"); break; }}

更多注解

  • @BindView—->绑定一个view;id为一个view 变量
  • @BindViews —-> 绑定多个view;id为一个view的list变量
  • @BindArray—-> 绑定string里面array数组;@BindArray(R.array.city ) String[] citys ;
  • @BindBitmap—->绑定图片资源为Bitmap;@BindBitmap( R.mipmap.wifi ) Bitmap bitmap;
  • @BindBool —->绑定boolean值
  • @BindColor —->绑定color;@BindColor(R.color.colorAccent) int black;
  • @BindDimen —->绑定Dimen;@BindDimen(R.dimen.borth_width) int mBorderWidth;
  • @BindDrawable —-> 绑定Drawable;@BindDrawable(R.drawable.test_pic) Drawable mTestPic;
  • @BindFloat —->绑定float
  • @BindInt —->绑定int
  • @BindString —->绑定一个String id为一个String变量;@BindString( R.string.app_name ) String meg;

更多事件

  • @OnClick—->点击事件
  • @OnCheckedChanged —->选中,取消选中
  • @OnEditorAction —->软键盘的功能键
  • @OnFocusChange —->焦点改变
  • @OnItemClick item—->被点击(注意这里有坑,如果item里面有Button等这些有点击的控件事件的,需要设置这些控件属性focusable为false)
  • @OnItemLongClick item—->长按(返回真可以拦截onItemClick)
  • @OnItemSelected —->item被选择事件
  • @OnLongClick —->长按事件
  • @OnPageChange —->页面改变事件
  • @OnTextChanged —->EditText里面的文本变化事件
  • @OnTouch —->触摸事件
  • @Optional —->选择性注入,如果当前对象不存在,就会抛出一个异常,为了压制这个异常,可以在变量或者方法上加入一下注解,让注入变成选择性的,如果目标View存在,则注入, 不存在,则什么事情都不做

ButterKnife的代码混淆

-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$ViewBinder { *; } -keepclasseswithmembernames class * {@butterknife.* <fields>;
}
-keepclasseswithmembernames class * {@butterknife.* <methods>;
}

Butterknife插件:zelezny

安装完成插件后,会提示重启AS。重启完后,可以写一个布局并且新建一个代码类测试下。

要注意的是,需要将光标移到setContentView(R.layout.acty_login),中的R.layout.acty_login,然后右键Generate就有了。


赠送源码:https://github.com/yugu88/MagicWX。

《最完整的Android逆向知识体系》

最新Butterknife集成 全部方法(完整版)相关推荐

  1. lut及3D LUT Mac调色预设如何导入并应用?关于fcpx/PR/AE/PS/LR/达芬奇lut预设导入及使用方法完整版介绍!

    lut预设是很多设计师用来编辑视频后期色调和图像调色的插件,通过使用LUT可以迅速达到很好的胶片质感和色彩,在此基础上稍作调整就能呈现很赞的色彩风格,lut预设格式为.cube.那么lut预设怎么用? ...

  2. 【144页/附下载】5G最新进展深度解析—全集完整版

    | 文章版权所有,未经授权请勿转载或使用 「5G行业应用」相继推出<5G最新进展深度解析--全球市场篇>.<5G最新进展深度解析--国内市场篇>.<5G最新进展深度解析- ...

  3. 2023年SCI期刊最新影响因子发布Excel完整版下载(亲测可下载)

    2023年 SCI期刊  最新影响因子  发布   Excel   完整版  下载   (亲测可下载) 北京时间6月28日,2023年最新SCI影响因子(Impact Factor,IF)正式出炉.科 ...

  4. 最新码支付源码+完整版+免挂监听回调+微信、支付宝、qq监控APP打包教程

    码支付手机APP打包教程 码支付官网:https://pay.madanbao.com 1.打开uniapp的官网注册一个账号,网址为:www.dcloud.io/ 3.打开工具导入项目,打开mani ...

  5. 2018最新云课堂数据分析师完整版

    目录 ├─第1章 数据能做什么? │  ├─第一章 数据能做什么?.png │  ├─课程1.1 优秀数据分析师的三个特点.mp4 │  ├─课程1.2 避免对数据可视化的误解(上).mp4 │  ├ ...

  6. springboot dubbo引入包_spring boot 集成 dubbo 企业完整版

    一.什么是Spring Boot ? 现阶段的 Spring Boot 可谓是太火了,为什么呢?因为使用方便.配置简洁.上手快速,那么它是什么?从官网上我们可以看到,它是 Spring 开源组织下的一 ...

  7. 2018最新廖雪峰全套Java完整版

    # -*- coding: utf-8 -*- """ Created on Sat Nov 17 08:40:21 2018 @author: shenfangyuan ...

  8. Dll注入经典方法完整版

    注入Dll: 1,OpenProcess获得要注入进程的句柄 2,VirtualAllocEx在远程进程中开辟出一段内存,长度为strlen(dllname)+1; 3,WriteProcessMem ...

  9. 百度SEO站群最新易支付源码完整版 已pj全解密

    简单的介绍一下测试结果吧 源码介绍: (1)20套首页模板 (2)用户排名榜板块 (3)广推引流一个模块 (4)邮箱配置 (5)短信配置 (6)结算配置 (7)商品拦截 (8)用户登录问候语音 (9) ...

最新文章

  1. solidworks模板_SolidWorks文件属性分类和创建方法,图纸自动属性的基础
  2. java最大子方阵_Java实验(5) 最大子方阵
  3. 关于昌平100度健身俱乐部全民健身情况调查报告
  4. 【2020牛客NOIP赛前集训营-提高组(第二场)】题解(GCD,包含,前缀,移动)
  5. 深入理解CRITICAL_SECTION
  6. 作者:王倩(1983-),女,上海计算机软件技术开发中心工程师。
  7. python字符串处理函数汇总_python字符串函数总结
  8. 笨办法学 Python · 续 练习 7:`grep`
  9. 情人节表白代码(2)
  10. 构建嵌入式LINUX的NFS【ZT】
  11. java围棋毕业设计_(毕业论文)围棋游戏的设计与实现.doc
  12. 火爆全网MySQL路线笔记!java静态变量和实例变量
  13. python模块文件的扩展名不一定是py_Python导入:导入没有.py扩展名的模块?
  14. 让A超链接无效的办法 阻止元素发生默认的行为
  15. 重磅!吴恩达新书《机器学习训练秘籍》中文版来了(附PDF下载)
  16. 深入理解java:2.3.4. 并发编程concurrent包 之容器ConcurrentLinkedQueue(非阻塞的并发队列---循环CAS)...
  17. 天气预报 API 各城市编码
  18. c语言中 cos函数图像,cos图像(cos函数的图像)
  19. 弘辽科技:拼多多批发价格会影响活动价格吗?商家要不要改价?
  20. C语言有哪些冷知识?

热门文章

  1. BackgroundWorker
  2. docker容器中安装vim 、telnet、ifconfig, ping命令
  3. P2817 宋荣子的城堡
  4. vue-cli中引入jquery方法
  5. 20145203 《信息安全系统设计基础》第十三周学习总结
  6. 超全!整理常用的iOS第三方资源
  7. Ida双开定位android so文件
  8. [Effective C++ --029]为“异常安全”而努力是值得的
  9. 安装Nginx时报错 the HTTP cache module requires md5 functions
  10. 两条实用的 SQL 语句