赞
踩
ButterKnife非常好。
打开Android studio 左上角File-settings-选择Plugins搜索Android ButterKnife Zelezny然后安装重新启动android studio
mobule的build.gradle文件的添加依赖库:
apply plugin:'android-apt'
compile'com.jakewharton:butterknife:8.4.0'
apt'com.jakewharton:butterknife-compiler:8.4.0'
根项目的build.gradle文件中添加:
classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0'
classpath'com.neenbedankt.gradle.plugins:android-apt:1.4'
注意如果在Library 项目中使用要按如下步骤:
(1). 经典用法图:
(2). 绑定Activity或Fragment
由于每次都要在Activity中的onCreate绑定Activity,所以个人建议写一个BaseActivity完成绑定,子类继承即可
BaseActivity:
public abstract class BaseActivity extends Activity {
public abstract int getContentViewId();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getContentViewId());
ButterKnife.bind(this);
initAllMembersView(savedInstanceState);
}
protected abstract void initAllMembersView(Bundle savedInstanceState);
@Override
protected void onDestroy() {
super.onDestroy();
ButterKnife.unbind(this);//解除绑定,官方文档只对fragment做了解绑
}
}
BaseFragment:
public abstract class BaseFragment extends Fragment {
public abstract int getContentViewId();
protected Context context;
protected View mRootView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mRootView =inflater.inflate(getContentViewId(),container,false);
ButterKnife.bind(this,mRootView);//绑定framgent
this.context = getActivity();
initAllMembersView(savedInstanceState);
return mRootView;
}
protected abstract void initAllMembersView(Bundle savedInstanceState);
@Override
public void onDestroyView() {
super.onDestroyView();
ButterKnife.unbind(this);//解绑
}
}
(3). 其他
@Bind
TextView mTextView//最常用的注解,用来绑定View,避免findViewById,也可以用在ViewHolder里,必须是public
@Bind({ R.id.first_name, R.id.middle_name, R.id.last_name })
List<EditText> nameViews//绑定多个view,只能用List不能用ArrayList
@OnClick(R.id.submit)
public void submit(View view) {...}//绑定点击事件,支持多个id绑定同一个方法
@OnItemSelected(R.id.list_view)
void onItemSelected(int position) {...}//selected事件
@OnItemClick(R.id.example_list)
void onItemClick(int position) {...}//itemClick事件
@OnFocusChange(R.id.example)
void onFocusChanged(boolean focused){...}//焦点改变监听
@OnItemLongClick(R.id.example_list)
boolean onItemLongClick(int position){...}//长按监听
@OnPageChange(R.id.example_pager)
void onPageSelected(int position){...}//Viewpager切换监听
@OnTextChanged(R.id.example)
void onTextChanged(CharSequence text)//内容改变监听
绑定资源:
@BindInt//用来绑定Integer类型的resource ID
@BindString//用来绑定string.xml里的字符串
@BindDrawable//用来绑定图片
@BindColor//用来绑定颜色
@BindDimen//用来绑定dimens
对一组View进行统一操作
@Bind({ R.id.first_name, R.id.middle_name, R.id.last_name })
List<EditText> nameViews;//装入一个list
static final ButterKnife.Action<View> DISABLE = new ButterKnife.Action<View>() {
@Override public void apply(View view, int index) {
view.setEnabled(false);
}
};//设置统一处理
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);
}
};
ButterKnife.apply(nameViews, DISABLE);
ButterKnife.apply(nameViews, ENABLED, false);//统一操作处理,例如设置是否可点,属性等
默认情况下,“绑定”和“监听”绑定都是必需的。如果不能找到目标视图,则将抛出异常。所以做空处理
@Nullable @Bind(R.id.might_not_be_there)
TextView mightNotBeThere;
@Nullable @OnClick(R.id.maybe_missing)
void onMaybeMissingClicked() {
// TODO ...
}
空指针异常解决方案:
1.点开 app模块的gradle编译文件build.gradle,加入下列代码
apply plugin: 'com.neenbedankt.android-apt'
在 dependencies中加入
apt 'com.jakewharton:butterknife-compiler:8.4.0'
compile 'com.jakewharton:butterknife:8.4.0'
2.打开项目的gradle编译文件build.gradle文件,在 dependencies中加入
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
3.如果还没解决请叉掉这篇博客。不客气。
剑未佩妥,出门已是江湖。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。