Butterknife使用——转
轉(zhuǎn)載請標(biāo)明出處:http://blog.csdn.net/donkor_/article/details/77879630
前言:?
ButterKnife是一個(gè)專注于Android系統(tǒng)的View注入框架,以前總是要寫很多findViewById來找到View對象,有了ButterKnife可以很輕松的省去這些步驟。是大神JakeWharton的力作,目前使用很廣。最重要的一點(diǎn),使用ButterKnife對性能基本沒有損失,因?yàn)锽utterKnife用到的注解并不是在運(yùn)行時(shí)反射的,而是在編譯的時(shí)候生成新的class。項(xiàng)目集成起來也是特別方便,使用起來也是特別簡單。
通過學(xué)習(xí)本文,學(xué)會(huì)如何在項(xiàng)目中使用ButterKnife。本文包含以下要點(diǎn):
前言?
簡單介紹
ButterKnife的優(yōu)勢
基本配置
ButterKnife的注冊與綁定?
ButterKnife使用心得與注意事項(xiàng)
在Activity中綁定ButterKnife
在Fragment中綁定ButterKnife
在Adapter中綁定ButterKnife
ButterKnife的基本使用?
綁定View
綁定資源
事件綁定
綁定監(jiān)聽
使用findById
設(shè)置多個(gè)view的屬性
使用注意事項(xiàng)
更多綁定注解
更多事件注解
ButterKnife的代碼混淆
Butterknife插件:zelezny?
插件安裝
插件使用
ButterKnife項(xiàng)目地址:https://github.com/JakeWharton/butterknife
▲ ButterKnife的優(yōu)勢:
1、強(qiáng)大的View綁定和Click事件處理功能,簡化代碼,提升開發(fā)效率
2、方便的處理Adapter里的ViewHolder綁定問題
3、運(yùn)行時(shí)不會(huì)影響APP效率,使用配置方便
4、代碼清晰,可讀性強(qiáng)
基本配置?
在android Studio項(xiàng)目中配置使用ButterKnife
Step one:在Project的 build.gradle 中添加如下代碼:
buildscript {
? ? repositories {
? ? ? ? jcenter()
? ? }
? ? dependencies {
? ? ? ? classpath 'com.android.tools.build:gradle:2.3.3'
? ? ? ? classpath 'com.jakewharton:butterknife-gradle-plugin:8.8.1' ?//添加這一行
? ? }
}
1
2
3
4
5
6
7
8
9
Step two:在App的 build.gradle 中添加如下代碼:
apply plugin: 'com.jakewharton.butterknife'
1
dependencies中添加:
compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
1
2
構(gòu)建環(huán)境特別簡單,接下來看看如何使用
ButterKnife的注冊與綁定
▲ ButterKnife使用心得與注意事項(xiàng):
1、在Activity 類中綁定 :ButterKnife.bind(this);必須在setContentView();之后綁定;且父類bind綁定后,子類不需要再bind。
2、在非Activity 類(eg:Fragment、ViewHold)中綁定: ButterKnife.bind(this,view);這里的this不能替換成getActivity()。
3、在Activity中不需要做解綁操作,在Fragment 中必須在onDestroyView()中做解綁操作。
4、使用ButterKnife修飾的方法和控件,不能用private or static 修飾,否則會(huì)報(bào)錯(cuò)。錯(cuò)誤: @BindView fields must not be private or static. (com.zyj.wifi.ButterknifeActivity.button1)
5、setContentView()不能通過注解實(shí)現(xiàn)。(其他的有些注解框架可以)
6、使用Activity為根視圖綁定任意對象時(shí),如果你使用類似MVC的設(shè)計(jì)模式你可以在Activity 調(diào)用ButterKnife.bind(this, activity),來綁定Controller。
7、使用ButterKnife.bind(this,view)綁定一個(gè)view的子節(jié)點(diǎn)字段。如果你在子View的布局里或者自定義view的構(gòu)造方法里 使用了inflate,你可以立刻調(diào)用此方法。或者,從XML inflate來的自定義view類型可以在onFinishInflate回調(diào)方法中使用它。
▲ 在Activity中綁定ButterKnife:
由于每次都要在Activity中的onCreate綁定Activity,所以個(gè)人建議寫一個(gè)BaseActivity完成綁定,子類繼承即可。綁定Activity 必須在setContentView之后。使用ButterKnife.bind(this)進(jìn)行綁定。代碼如下:
public class MainActivity extends AppCompatActivity{ ?
? ? @Override ?
? ? protected void onCreate(Bundle savedInstanceState) { ?
? ? ? ? super.onCreate(savedInstanceState); ?
? ? ? ? setContentView(R.layout.activity_main); ?
? ? ? ? //綁定初始化ButterKnife ?
? ? ? ? ButterKnife.bind(this); ?
? ? } ?
} ?
1
2
3
4
5
6
7
8
9
▲ 在Fragment中綁定ButterKnife:
Fragment的生命周期不同于activity。在onCreateView中綁定一個(gè)Fragment時(shí),在onDestroyView中將視圖設(shè)置為null。當(dāng)你調(diào)用bind來為你綁定一個(gè)Fragment時(shí),Butter Knife會(huì)返回一個(gè)Unbinder的實(shí)例。在適當(dāng)?shù)纳芷?#xff08;onDestroyView)回調(diào)中調(diào)用它的unbind方法進(jìn)行Fragment解綁。使用ButterKnife.bind(this, view)進(jìn)行綁定。代碼如下:
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(); ?
? ? } ?
} ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
▲ 在Adapter中綁定ButterKnife:
在Adapter的ViewHolder中使用,將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); ?
? ? ? 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); ?
? ? } ?
? } ?
} ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
ButterKnife的基本使用
▲ 綁定View:
控件id 注解: @BindView()
@BindView( R2.id.button) ?
public Button button; ??
1
2
布局內(nèi)多個(gè)控件id 注解: @BindViews()
public class MainActivity extends AppCompatActivity { ?
? ? @BindViews({ R2.id.button1, R2.id.button2, ?R2.id.button3}) ?
? ? public List<Button> buttonList ; ?
? ? @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 "); ?
? ? } ?
} ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
▲ 綁定資源:
綁定string 字符串:@BindString()
public class MainActivity extends AppCompatActivity { ?
? ? @BindView(R2.id.button) //綁定button 控件 ?
? ? public Button button ; ?
? ? @BindString(R2.string.app_name) ?//綁定資源文件中string字符串 ?
? ? String str; ?
? ? @Override ?
? ? protected void onCreate(Bundle savedInstanceState) { ?
? ? ? ? super.onCreate(savedInstanceState); ?
? ? ? ? setContentView(R.layout.activity_main); ?
? ? ? ? //綁定activity ?
? ? ? ? ButterKnife.bind( this ) ; ?
? ? ? ? button.setText( str ); ?
? ? } ?
} ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
綁定string里面array數(shù)組:@BindArray()
<resources> ?
? ? <string name="app_name">城市</string> ?
? ? <string-array name="city"> ?
? ? ? ? <item>北京市</item> ?
? ? ? ? <item>天津市</item> ?
? ? ? ? <item>哈爾濱市</item> ?
? ? ? ? <item>大連市</item> ?
? ? ? ? <item>香港市</item> ?
? ? </string-array> ?
</resources> ?
------------------------------------------------------------------------------
public class MainActivity ?extends AppCompatActivity { ?
? ? @BindView(R2.id.button) //綁定button 控件 ?
? ? public Button button ; ?
? ? @BindString(R2.string.app_name) ?//綁定資源文件中string字符串 ?
? ? String str; ?
? ? @BindArray(R2.array.city) ?//綁定string里面array數(shù)組 ?
? ? String [] citys ; ?
? ? @Override ?
? ? protected void onCreate(Bundle savedInstanceState) { ?
? ? ? ? super.onCreate(savedInstanceState); ?
? ? ? ? setContentView(R.layout.activity_main); ?
? ? ? ? //綁定activity ?
? ? ? ? ButterKnife.bind( this ) ; ?
? ? ? ? button.setText(citys[0]); ?
? ? } ?
} ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
綁定Bitmap 資源:@BindBitmap( )
public class MainActivity extends AppCompatActivity { ?
? ? @BindView( R2.id.imageView ) //綁定ImageView 控件 ?
? ? public ImageView imageView ; ?
? ? @BindBitmap( R2.mipmap.bm)//綁定Bitmap 資源 ?
? ? public Bitmap bitmap ; ?
? ? @Override ?
? ? protected void onCreate(Bundle savedInstanceState) { ?
? ? ? ? super.onCreate(savedInstanceState); ?
? ? ? ? setContentView(R.layout.activity_main); ?
? ? ? ? //綁定activity ?
? ? ? ? ButterKnife.bind( this ) ; ?
? ? ? ? imageView.setImageBitmap(bitmap); ?
? ? } ?
} ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
綁定一個(gè)顏色值:@BindColor( )
public class MainActivity extends AppCompatActivity { ?
? ? @BindView( R2.id.button) ?//綁定一個(gè)控件 ?
? ? public Button button; ?
? ? @BindColor( R2.color.colorAccent ) //具體色值在color文件中 ?
? ? int black ; ?//綁定一個(gè)顏色值 ?
? ? @Override ?
? ? protected void onCreate(Bundle savedInstanceState) { ?
? ? ? ? super.onCreate(savedInstanceState); ?
? ? ? ? setContentView(R.layout.activity_main); ?
? ? ? ? //綁定activity ?
? ? ? ? ButterKnife.bind( this ) ; ?
? ? ? ? button.setTextColor( ?black ); ?
? ? } ?
} ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
▲ 事件綁定:
綁定點(diǎn)擊事件:
綁定控件點(diǎn)擊事件:@OnClick( )
綁定控件長按事件:@OnLongClick( )
public class MainActivity extends AppCompatActivity { ?
? ? @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è)長按事件 ?
? ? public boolean showToast2(){ ?
? ? ? ? Toast.makeText(this, "is a long click", Toast.LENGTH_SHORT).show(); ?
? ? ? ? return true ; ?
? ? } ?
? ? @Override ?
? ? protected void onCreate(Bundle savedInstanceState) { ?
? ? ? ? super.onCreate(savedInstanceState); ?
? ? ? ? setContentView(R.layout.activity_main); ?
? ? ? ? //綁定activity ?
? ? ? ? ButterKnife.bind( this ) ; ?
? ? } ?
} ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
指定多個(gè)id綁定事件:
public class MainActivity extends AppCompatActivity { ?
? ? //Tip:當(dāng)涉及綁定多個(gè)id事件時(shí),我們可以使用Android studio的Butterknife
? ? //插件zelezny快速自動(dòng)生成的,之后在下面會(huì)有介紹安裝插件與使用 ?
? ? @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; ?
? ? ? ? } ?
? ? } ?
? ? @Override ?
? ? protected void onCreate(Bundle savedInstanceState) { ?
? ? ? ? super.onCreate(savedInstanceState); ?
? ? ? ? setContentView(R.layout.activity_main); ?
? ? ? ? //綁定activity ?
? ? ? ? ButterKnife.bind( this ) ; ?
? ? } ?
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
通過上面的例子可以看出多條點(diǎn)擊事件是沒有用R2的方式,如果一定要使用R2的寫法,可以單一逐次寫,正確的寫法如下:
public class MainActivity extends AppCompatActivity { ? ?
? ? @OnClick(R2.id.ll_product_name) ? ?
? ? public void onViewClicked1(View view) { ? ?
? ? ? ?System.out.print("我是點(diǎn)擊事件1"); ? ? ? ? ? ? ??
? ? } ? ?
? ? @OnClick(R2.id.ll_product_lilv) ? ?
? ? public void onViewClicked2(View view) { ? ?
? ? ? ?System.out.print("我是點(diǎn)擊事件2"); ? ??
? ? } ??
? ? @OnClick(R2.id.ll_product_qixian) ? ?
? ? public void onViewClicked3(View view) { ? ?
? ? ? ?System.out.print("我是點(diǎn)擊事件3"); ? ? ? ? ? ? ??
? ? } ? ?
? ? @OnClick(R2.id.ll_product_repayment_methods) ? ?
? ? public void onViewClicked4(View view) { ? ?
? ? ? ?System.out.print("我是點(diǎn)擊事件4"); ? ? ? ? ? ? ??
? ? } ? ?
? ? @Override ? ?
? ? protected void onCreate(Bundle savedInstanceState) { ? ?
? ? ? ? super.onCreate(savedInstanceState); ? ?
? ? ? ? setContentView(R.layout.activity_main); ? ?
? ? ? ? //綁定activity ? ?
? ? ? ? ButterKnife.bind( this ) ; ? ?
? ? } ? ?
} ? ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
自定義View使用綁定事件
不用指定id,直接注解OnClick。看代碼覺得好像跟實(shí)現(xiàn)點(diǎn)擊事件的方法類似。實(shí)際上并沒有實(shí)現(xiàn)OnClickListener接口。代碼如下:
public class MyButton extends Button { ?
? @OnClick ?
? public void onClick() {} ?
} ?
1
2
3
4
▲ 綁定監(jiān)聽:
Listeners可以自動(dòng)配置到方法中
@OnClick(R.id.submit) ?
public void submit(View view) { ?
? // TODO submit data to server... ?
} ?
1
2
3
4
對監(jiān)聽器方法的所有參數(shù)都是可選的
@OnClick(R.id.submit) ?
public void submit() { ?
? // TODO submit data to server... ?
} ?
1
2
3
4
自定義一個(gè)特定類型,它將自動(dòng)被轉(zhuǎn)換
@OnClick(R.id.submit) ?
? ? public void sayHi(Button button) {//看括號(hào)內(nèi)參數(shù)的變化就明白了 ?
? ? ? button.setText("Hello!"); ?
? ? } ?
1
2
3
4
在單個(gè)綁定中指定多個(gè)id,用于公共事件處理。這里舉例點(diǎn)擊事件。其他的事件監(jiān)聽同樣也是可以的。
@OnClick(R.id.submitCode,R.id.submitFile,R.id.submitTest) ?
? ? public void sayHi(Button button) {//多個(gè)控件對應(yīng)公共事件
? ? ? button.setText("Success!"); ?
? ? } ?
1
2
3
4
自定義視圖可以通過不指定ID來綁定到它們自己的監(jiān)聽器。
public class FancyButton extends Button { ?
? @OnClick ?
? public void onClick() { ?
? ? // TODO do something! ?
? } ?
}
1
2
3
4
5
6
Listener中多方法注解
方法注解,其對應(yīng)的監(jiān)聽器有多個(gè)回調(diào),可用于綁定到其中任何一個(gè)。每個(gè)注解都有一個(gè)它綁定的默認(rèn)回調(diào)。使用回調(diào)參數(shù)指定一個(gè)替換。以Spinner為例。?
原本代碼:
Spinner s=new Spinner(this); ?
? ? ? ?//原始方法:Spinner 條目選擇監(jiān)聽事件 正常寫法 ?
? ? ? ?s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){ ?
? ? ? ? ? ?@Override ?
? ? ? ? ? ?public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { ?
? ? ? ? ? ?} ?
? ? ? ? ? ?@Override ?
? ? ? ? ? ?public void onNothingSelected(AdapterView<?> parent) { ?
? ? ? ? ? ?} ?
? ? ? ?});?
1
2
3
4
5
6
7
8
9
10
通過 Butter Knife 注解方式
public class MainActivity extends AppCompatActivity { ?
? ? /*利用注解對Spinner item ?作選擇監(jiān)聽事件處理方式*/ ?
? ? @OnItemSelected(R.id.my_spiner)//默認(rèn)callback為ITEM_SELECTED ?
? ? void onItemSelected(int position) { ?
? ? ? ? Toast.makeText(this, "position: " + position, Toast.LENGTH_SHORT).show(); ?
? ? } ?
? ? /*?
? ? * 注解onNothingSelected,需要在注解參數(shù)添加一個(gè)callback,?
? ? * 注意的是Spinner中只要有數(shù)據(jù),默認(rèn)都會(huì)選中第0個(gè)數(shù)據(jù),所以想進(jìn)入到onNothingSelected()方法,就需要把Adapter中的數(shù)據(jù)都清空?
? ? */ ?
? ? @OnItemSelected(value = R.id.my_spiner, callback = OnItemSelected.Callback.NOTHING_SELECTED) ?
? ? void onNothingSelected() { ?
? ? ? ? Toast.makeText(this, "Nothing", Toast.LENGTH_SHORT).show(); ?
? ? } ?
? ? @Override ?
? ? protected void onCreate(Bundle savedInstanceState) { ?
? ? ? ? super.onCreate(savedInstanceState); ?
? ? ? ? setContentView(R.layout.activity_main); ?
? ? ? ? //綁定activity ?
? ? ? ? ButterKnife.bind( this ) ; ?
? ? ? ? Spinner s=new Spinner(this); ?
? ? } ?
} ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@OnCheckedChanged監(jiān)聽的使用
原方法應(yīng)是:setOnCheckedChangeListener()。使用栗子
<?xml version="1.0" encoding="utf-8"?> ?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ?
? ? android:layout_width="match_parent" ?
? ? android:layout_height="match_parent" ?
? ? android:orientation="vertical"> ?
? ? <RadioGroup ?
? ? ? ? android:id="@+id/rg_main" ?
? ? ? ? android:layout_width="fill_parent" ?
? ? ? ? android:layout_height="48dp" ?
? ? ? ? android:layout_alignParentBottom="true" ?
? ? ? ? android:background="@color/white" ?
? ? ? ? android:orientation="horizontal"> ?
? ? ? ? <RadioButton ?
? ? ? ? ? ? android:id="@+id/rg_home" ?
? ? ? ? ? ? android:layout_width="match_parent" ?
? ? ? ? ? ? android:layout_height="match_parent" ?
? ? ? ? ? ? android:focusable="false" ?
? ? ? ? ? ? android:text="@string/nav_one" /> ?
? ? ? ? <RadioButton ?
? ? ? ? ? ? android:id="@+id/rg_wealth" ?
? ? ? ? ? ? android:layout_width="match_parent" ?
? ? ? ? ? ? android:layout_height="match_parent" ?
? ? ? ? ? ? android:focusable="false" ?
? ? ? ? ? ? android:text="@string/nav_two" /> ?
? ? ? ? <RadioButton ?
? ? ? ? ? ? android:id="@+id/rg_account" ?
? ? ? ? ? ? android:layout_width="match_parent" ?
? ? ? ? ? ? android:layout_height="match_parent" ?
? ? ? ? ? ? android:focusable="false" ?
? ? ? ? ? ? android:text="@string/nav_four" /> ?
? ? </RadioGroup> ?
</LinearLayout> ?
-------------------------------------------------------------------------
@OnCheckedChanged({R.id.rg_home,R.id.rg_wealth,R.id.rg_account}) ?
? ? public void OnCheckedChangeListener(CompoundButton view, boolean ischanged ){ ?
? ? ? ? switch (view.getId()) { ?
? ? ? ? ? ? case R.id.rg_home: ?
? ? ? ? ? ? ? ? if (ischanged){//注意:這里一定要有這個(gè)判斷,只有對應(yīng)該id的按鈕被點(diǎn)擊了,ischanged狀態(tài)發(fā)生改變,才會(huì)執(zhí)行下面的內(nèi)容 ?
? ? ? ? ? ? ? ? ? ? //這里寫你的按鈕變化狀態(tài)的UI及相關(guān)邏輯 ?
? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? ? break; ?
? ? ? ? ? ? case R.id.rg_wealth: ?
? ? ? ? ? ? ? ? if (ischanged) { ?
? ? ? ? ? ? ? ? ? ? //這里寫你的按鈕變化狀態(tài)的UI及相關(guān)邏輯 ?
? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? ? break; ?
? ? ? ? ? ? case R.id.rg_account: ?
? ? ? ? ? ? ? ? if (ischanged) { ?
? ? ? ? ? ? ? ? ? ? //這里寫你的按鈕變化狀態(tài)的UI及相關(guān)邏輯 ?
? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? ? break; ?
? ? ? ? ? ? default: ?
? ? ? ? ? ? ? ? break; ?
? ? ? ? } ?
? ? } ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
▲ 使用findById:
Butter Knife仍然包含了findById()方法,用于仍需從一個(gè)view ,Activity,或者Dialog上初始化view的時(shí)候,并且它可以自動(dòng)轉(zhuǎn)換類型。
View view = LayoutInflater.from(context).inflate(R.layout.thing, null); ?
TextView firstName = ButterKnife.findById(view, R.id.first_name); ?
TextView lastName = ButterKnife.findById(view, R.id.last_name); ?
ImageView iv = ButterKnife.findById(view, R.id.iv); ?
1
2
3
4
▲ 設(shè)置多個(gè)view的屬性:
apply()
作用:允許您立即對列表中的所有視圖進(jìn)行操作。
Action和Setter接口
作用:Action和Setter接口允許指定簡單的行為。
public class MainActivity extends AppCompatActivity { ?
? ? @BindViews({R2.id.first_name, R2.id.middle_name, R2.id.last_name}) ?
? ? List<EditText> nameViews; ?
? ? @Override ?
? ? protected void onCreate(Bundle savedInstanceState) { ?
? ? ? ? super.onCreate(savedInstanceState); ?
? ? ? ? setContentView(R.layout.activity_main); ?
? ? ? ? //綁定activity ?
? ? ? ? ButterKnife.bind(this); ?
? ? ? ? //設(shè)置多個(gè)view的屬性 ?
? ? ? ? //方式1:傳遞值 ?
? ? ? ? ButterKnife.apply(nameViews, DISABLE); ?
? ? ? ? //方式2:指定值 ?
? ? ? ? ButterKnife.apply(nameViews, ENABLED, false); ?
? ? ? ? 方式3 設(shè)置View的Property ?
? ? ? ? ButterKnife.apply(nameViews, View.ALPHA, 0.0f);//一個(gè)Android屬性也可以用于應(yīng)用的方法。 ?
? ? } ?
? ? /*?
? ? * Action接口設(shè)置屬性?
? ? */ ?
? ? static final ButterKnife.Action<View> DISABLE = new ButterKnife.Action<View>() { ?
? ? ? ? @Override ?
? ? ? ? public void apply(View view, int index) { ?
? ? ? ? ? ? view.setEnabled(false);//目的是使多個(gè)view都具備此屬性 ?
? ? ? ? } ?
? ? }; ?
? ? /*?
? ? * Setter接口設(shè)置屬性?
? ? */ ?
? ? 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);//目的是使多個(gè)view都具備此屬性,可變boolean值是可以傳遞的 ?
? ? ? ? } ?
? ? }; ?
} ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
▲ 使用注意事項(xiàng):
ButterKinfe的注解標(biāo)簽因版本不同而有所變化。?
8.0.0之前的Bind標(biāo)簽在8.0.0之后變成了BindView,而8.7.0之后在綁定view時(shí),要用R2.id.XXX而不再是常用的R.id.XXX了。
具體變化情況和查看gitHub上的提交日志:?
https://github.com/JakeWharton/butterknife/blob/master/CHANGELOG.md#version-800-2016-04-25
默認(rèn)情況下,@bind和 listener 的綁定是必需的。如果無法找到目標(biāo)視圖,將拋出一個(gè)異常。?
要抑制此行為并創(chuàng)建可選綁定,可以將@Nullable注解添加到字段中,或?qū)?#64;Optional注解添加到方法。
任何被命名為@Nullable的注解都可以用于成員變量。建議使用android的”support-annotations”庫中的@Nullable注解。
@Nullable ?
@BindView(R.id.might_not_be_there) ??
TextView mightNotBeThere; ?
@Optional ?
@OnClick(R.id.maybe_missing) ??
public void onMaybeMissingClicked() { ?
? ? // TODO ... ?
} ?
1
2
3
4
5
6
7
8
9
▲ 更多綁定注解:
@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—->長按(返回真可以攔截onItemClick)
@OnItemSelected —->item被選擇事件
@OnLongClick —->長按事件
@OnPageChange —->頁面改變事件
@OnTextChanged —->EditText里面的文本變化事件
@OnTouch —->觸摸事件
@Optional —->選擇性注入,如果當(dāng)前對象不存在,就會(huì)拋出一個(gè)異常,為了壓制這個(gè)異常,可以在變量或者方法上加入一下注解,讓注入變成選擇性的,如果目標(biāo)View存在,則注入, 不存在,則什么事情都不做
//Test @Optional
@Optional ?
@OnCheckedChanged(R.id.cb_test) ?
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){ ?
? ? if(isChecked){ ?
? ? ? ? tvTest.setText("被選中..."); ?
? ? }else{ ?
? ? ? ? tvTest.setText("被取消..."); ?
? ? } ?
} ?
1
2
3
4
5
6
7
8
9
10
ButterKnife的代碼混淆
在混淆文件中,添加如下代碼:
-keep class butterknife.** { *; } ?
-dontwarn butterknife.internal.** ?
-keep class **$$ViewBinder { *; } ?
-keepclasseswithmembernames class * { ?
? ? @butterknife.* <fields>; ?
} ?
-keepclasseswithmembernames class * { ?
? ? @butterknife.* <methods>; ?
} ?
1
2
3
4
5
6
7
8
9
10
11
12
Butterknife插件:zelezny
插件安裝:
工具欄File 找到Settings…或者使用快捷點(diǎn)Ctrl+Alt+s 打開。搜索zelezny下載插件并安裝,重啟Android Studio
插件使用:
安裝完成插件后,會(huì)提示重啟AS,重啟完后,可以寫一個(gè)布局并且新建一個(gè)代碼類測試下。測試的過程中要注意的是,需要將光標(biāo)移到setContentView(R.layout.acty_login),將光標(biāo)放到R.layout.acty_login,然后右鍵Generate就有了。這里引用一張gif效果圖,更直觀表現(xiàn)出插件帶來的優(yōu)勢。對于多個(gè)需要綁定的id,省下了需要自己手動(dòng)敲打代碼的時(shí)間。
?
總結(jié)
以上是生活随笔為你收集整理的Butterknife使用——转的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: git远程仓库上传及本地仓库创建
- 下一篇: 大人物剧情介绍