引言
作为一个程序员,特别是一个app程序员,在写代码的时候,很大部分时间都花在布局的编写及控件的初始化和事件监听方法上了,这样就没有时间来处理代码逻辑和结构了,现在ButterKnife框架可以为你节省很多时间,真的很节省的.
Android Studio中使用ButterKnife
1.可以直接在build.gradle中配置
top build.gradle
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
app build.gradle
apply plugin: 'com.neenbedankt.android-apt'
dependencies {
...
compile 'com.jakewharton:butterknife:8.4.0'
apt 'com.jakewharton:butterknife-compiler:8.4.0'
}
1.也可以通过File->Project Structure -> app -> Dependencides-> + 搜索butterknife的最新版本.
然后就可以使用了:
package com.example.fishmov.daggerdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "fish---";
protected Unbinder unbinder;
@BindView(R.id.btn)
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
unbinder = ButterKnife.bind(this);
Log.i(TAG, "" + button);
}
@Override
protected void onDestroy() {
super.onDestroy();
unbinder.unbind();
}
}
注意:一定要在setContentView(R.layout.activity_main)后进行ButterKnife.bind(this);否则将报异常,当然我们会在onDestroy中进行unbinder.unbind().
对于这种处理,我们在app中可以采用一个基类的做法,避免重复编码,比如:
Activity
package com.example.fishmov.daggerdemo;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import butterknife.ButterKnife;
import butterknife.Unbinder;
/**
* Created by fishmov on 17-6-22.
*/
public abstract class BaseActivity extends AppCompatActivity {
protected abstract int getContentViewId();
protected abstract void initView();
protected Unbinder unbinder;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getContentViewId());
unbinder = ButterKnife.bind(this);
initView();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (null != unbinder) {
unbinder.unbind();
}
}
}
Fragment
package com.example.fishmov.daggerdemo;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import butterknife.ButterKnife;
import butterknife.Unbinder;
/**
* Created by fishmov on 17-6-22.
*/
public abstract class BaseFragment extends Fragment {
protected abstract int getContentViewId();
protected abstract void initView();
protected Unbinder unbinder;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(getContentViewId(), container, false);
unbinder = ButterKnife.bind(this, view);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initView();
}
@Override
public void onDestroyView() {
if (null != unbinder) {
unbinder.unbind();
}
super.onDestroyView();
}
}
ViewHolder
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.whatever, parent, false);
holder = new ViewHolder(view);
view.setTag(holder);
}
holder.name.setText("John Doe");
// etc...
return view;
}
static class ViewHolder {
@BindView(R.id.title) TextView name;
@BindView(R.id.job_title) TextView jobTitle;
public ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}
由于butterknife的用法比较简单,也很好理解,后面将不再以实例说明,只提供相关用法:
@BindView(R.id.title) TextView title;
@BindString(R.string.title) String title;
@BindDrawable(R.drawable.graphic) Drawable graphic;
@BindColor(R.color.red) int red; // int or ColorStateList field
@BindDimen(R.dimen.spacer) Float spacer; // int (for pixel size) or float (for exact value) field
一篇很好的文章:http://blog.csdn.net/itjianghuxiaoxiong/article/details/50177549
butterknife官方说明:http://jakewharton.github.io/butterknife/