最新Butterknife集成 全部方法(完整版)
生活随笔
收集整理的這篇文章主要介紹了
最新Butterknife集成 全部方法(完整版)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
贈(zèng)送源碼:https://github.com/yugu88/MagicWX。
《最完整的Android逆向知識(shí)體系》
集成分為了兩部分:
①僅僅在App主工程使用:
在App的 build.gradle 中添加如下代碼:版本號(hào)9之前的沒有適配AndroidX,工程遷移AndroidX時(shí)會(huì)報(bào)錯(cuò)。
dependencies {implementation 'com.jakewharton:butterknife:9.0.0-rc3'// 必須要有,不然無法綁定點(diǎn)擊事件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'// 必須要有,不然無法綁定點(diǎn)擊事件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 注意事項(xiàng):
- 在Activity 類中綁定 :ButterKnife.bind(this);必須在setContentView();之后綁定;且父類bind綁定后,子類不需要再bind。
- 在非Activity 類(eg:Fragment、ViewHold)中綁定: ButterKnife.bind(this,view);這里的this不能替換成getActivity()。
- 在Activity中不需要做解綁操作,在Fragment 中必須在onDestroyView()中做解綁操作。
- 使用ButterKnife修飾的方法和控件,不能用private or static 修飾,否則會(huì)報(bào)錯(cuò)。錯(cuò)誤: @BindView fields must not be private or static. (com.zyj.wifi.ButterknifeActivity.button1)
- setContentView()不能通過注解實(shí)現(xiàn)。(其他的有些注解框架可以)
- 使用Activity為根視圖綁定任意對(duì)象時(shí),如果你使用類似MVC的設(shè)計(jì)模式你可以在Activity 調(diào)用ButterKnife.bind(this, activity),來綁定Controller。
- 使用ButterKnife.bind(this,view)綁定一個(gè)view的子節(jié)點(diǎn)字段。如果你在子View的布局里或者自定義view的構(gòu)造方法里 使用了inflate,你可以立刻調(diào)用此方法。或者,從XML inflate來的自定義view類型可以在onFinishInflate回調(diào)方法中使用它。
在Activity中綁定ButterKnife:
public class MainActivity extends AppCompatActivity{ @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //綁定初始化ButterKnife 必須在setContentView之后調(diào)用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); //返回一個(gè)Unbinder值(進(jìn)行解綁),注意這里的this不能使用getActivity() unbinder = ButterKnife.bind(this, view); return view;} /** * onDestroyView中進(jìn)行解綁操作 */ @Override public void onDestroyView() { super.onDestroyView(); unbinder.unbind(); } }在Adapter中綁定ButterKnife:
將ViewHolder加一個(gè)構(gòu)造方法,在new ViewHolder的時(shí)候把view傳遞進(jìn)去。使用ButterKnife.bind(this, view)進(jìn)行綁定。
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加一個(gè)構(gòu)造方法,在new ViewHolder的時(shí)候把view傳遞進(jìn)去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)進(jìn)行綁定ButterKnife.bind(this, view); } } }ButterKnife的基本使用
/** * 主App工程使用如下 */ @BindView(R.id.txt_tips) TextView mTxtTips;/** * library工程使用如下 */ @BindView( R2.id.button) Button button; // 就是R和R2的區(qū)別,因?yàn)閘ibrary中目前不能調(diào)用R文件了。// 同時(shí)綁定多個(gè) @BindViews({ R2.id.button1, R2.id.button2, R2.id.button3}) public List<Button> buttonList ; //綁定資源文件中string字符串 @BindString(R2.string.app_name) String str; //綁定資源文件中string里面array數(shù)組 @BindArray(R2.array.city) String [] citys ; //綁定Bitmap 資源 @BindBitmap( R2.mipmap.bm)public Bitmap bitmap ; //具體色值在color文件中 @BindColor( R2.color.colorAccent ) int black; //綁定一個(gè)顏色值 @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 設(shè)置一個(gè)點(diǎn)擊事件 public void showToast(){ Toast.makeText(this, "is a click", Toast.LENGTH_SHORT).show(); } @OnLongClick( R2.id.button1 ) //給 button1 設(shè)置一個(gè)長(zhǎng)按事件 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("我是點(diǎn)擊事件1"); break;case R.id.ll_product_lilv: System.out.print("我是點(diǎn)擊事件2"); break; case R.id.ll_product_qixian: System.out.print("我是點(diǎn)擊事件3"); break; case R.id.ll_product_repayment_methods: System.out.print("我是點(diǎn)擊事件4"); break; }}更多注解
- @BindView—->綁定一個(gè)view;id為一個(gè)view 變量
- @BindViews —-> 綁定多個(gè)view;id為一個(gè)view的list變量
- @BindArray—-> 綁定string里面array數(shù)組;@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 —->綁定一個(gè)String id為一個(gè)String變量;@BindString( R.string.app_name ) String meg;
更多事件
- @OnClick—->點(diǎn)擊事件
- @OnCheckedChanged —->選中,取消選中
- @OnEditorAction —->軟鍵盤的功能鍵
- @OnFocusChange —->焦點(diǎn)改變
- @OnItemClick item—->被點(diǎn)擊(注意這里有坑,如果item里面有Button等這些有點(diǎn)擊的控件事件的,需要設(shè)置這些控件屬性focusable為false)
- @OnItemLongClick item—->長(zhǎng)按(返回真可以攔截onItemClick)
- @OnItemSelected —->item被選擇事件
- @OnLongClick —->長(zhǎng)按事件
- @OnPageChange —->頁面改變事件
- @OnTextChanged —->EditText里面的文本變化事件
- @OnTouch —->觸摸事件
- @Optional —->選擇性注入,如果當(dāng)前對(duì)象不存在,就會(huì)拋出一個(gè)異常,為了壓制這個(gè)異常,可以在變量或者方法上加入一下注解,讓注入變成選擇性的,如果目標(biāo)View存在,則注入, 不存在,則什么事情都不做
ButterKnife的代碼混淆
-keep class butterknife.** { *; } -dontwarn butterknife.internal.** -keep class **$$ViewBinder { *; } -keepclasseswithmembernames class * {@butterknife.* <fields>; } -keepclasseswithmembernames class * {@butterknife.* <methods>; }Butterknife插件:zelezny
安裝完成插件后,會(huì)提示重啟AS。重啟完后,可以寫一個(gè)布局并且新建一個(gè)代碼類測(cè)試下。
要注意的是,需要將光標(biāo)移到setContentView(R.layout.acty_login),中的R.layout.acty_login,然后右鍵Generate就有了。
贈(zèng)送源碼:https://github.com/yugu88/MagicWX。
《最完整的Android逆向知識(shí)體系》
與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的最新Butterknife集成 全部方法(完整版)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android studio 混淆打包
- 下一篇: gradle使用技巧之全局变量