android使用 注解框架,Android实践 | 注解框架ButterKnife基本使用
使用ButterKnife,我們可以不用寫很多的findViewById()語(yǔ)句,以及通過getResources獲取String、Color等資源,這可以讓我們的代碼更加簡(jiǎn)潔,使用起來也很方便。下面來看怎么用吧!
首先當(dāng)然是添加依賴(建議去github查看最新版本)
implementation 'com.jakewharton:butterknife:9.0.0-rc2'
annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-rc2'
接下來要在Activity中進(jìn)行綁定:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
需要注意的是:
Butterkinife.bind(this) 必須在setContentView()后面;
setContentView()不能通過注解實(shí)現(xiàn);
在父類中綁定后,就不用在子類中再進(jìn)行綁定了(因此,建議寫一個(gè)BaseActivity,進(jìn)行Activity綁定,然后讓所有的Activity去繼承BaseActivity)。
綁定控件
通常操作:
Button button = (Button)findViewById(R.id.button);
使用ButterKnife:
@BindView(R.id.button) Button button;
這里需要注意:使用ButterKnife注解的控件或資源不能用private等修飾符。
綁定資源
通常操作:
String str = getResources().getString(R.string.app_name);
int color = getResources().getColor(R.color.colorPrimary);
Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher_background);
使用ButterKnife:
@BindString(R.string.app_name) String str;
@BindColor(R.color.colorPrimary) int color;
@BindDrawable(R.drawable.ic_launcher_background) Drawable drawable;
是不是高端大氣上檔次,簡(jiǎn)潔明了不啰嗦!!!
當(dāng)然,在Fragment中使用也不是問題
public class LeftFragment extends Fragment {
@BindView(R.id.button)
Button button;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_left, container, false);
ButterKnife.bind(this, view);
return view;
}
}
不過在Fragment中使用ButterKinfe時(shí),我們需要在onDestroy方法中對(duì)其進(jìn)行解綁,因此在Fragment中使用時(shí)我們要這樣寫:
public class LeftFragment extends Fragment {
@BindView(R.id.button)
Button button;
private Unbinder unbinder;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_left, container, false);
unbinder = ButterKnife.bind(this, view);
return view;
}
@Override
public void onDestroy() {
super.onDestroy();
unbinder.unbind();
}
}
當(dāng)我們調(diào)用 ButterKnife.bind() 方法時(shí),會(huì)返回一個(gè)Unbinder對(duì)象,我們只需要重寫onDestroy()方法,調(diào)用Unbinder對(duì)象的unbind()方法,進(jìn)行解綁。
在Adapter里面也是可以用的
public class ListAdapter extends BaseAdapter {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null){
convertView = mInflater.inflate(R.layout.item_article, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}else {
holder = (ViewHolder)convertView.getTag();
}
return convertView;
}
public final class ViewHolder{
@BindView(R.id.title) TextView mTitleText;
@BindView(R.id.inform) TextView mInformText;
public ViewHolder(View view){
ButterKnufe.bind(this, view)
}
}
}
總之,任何需要 findViewById() 的地方都可以使用ButterKnife來簡(jiǎn)化代碼。
前面只說了綁定View和資源,接下來當(dāng)然是Listener啦
單個(gè)View監(jiān)聽
@OnClick(R.id.show)
public void show(){
//TODO
}
@OnClick(R.id.show)
public void show(TextView textview){
//TODO
}
@OnClick(R.id.show)
public void show(View view){
//TODO
}
所有監(jiān)聽方法的參數(shù)都是可選的,可寫可不寫。
多個(gè)View監(jiān)聽
@OnClick({R.id.button1, R.id.button2, R.id.text})
public void show(View view){
switch (view.getId()){
case R.id.button1:
//TODO
break;
case R.id.button2:
//TODO
break;
case R.id.text:
//TODO
break;
default:
break;
}
}
當(dāng)然,這里所列舉出來的都是一些比較常用的基本用法,如果你想了解更多關(guān)于ButterKnife的用法,可以去看看 JakeWharton 給出的文檔。
補(bǔ)充:
如果在運(yùn)行時(shí)報(bào)這個(gè)錯(cuò)誤:
只需要在 app 的 build.gradle 文件中的 android 標(biāo)簽下 加上
android {
···
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
將JDK版本指定為1.8即可解決。
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的android使用 注解框架,Android实践 | 注解框架ButterKnife基本使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python调用库函数用ecb模式加密图
- 下一篇: 分屏 取消_记录Android7.0以上