Butter Knife(黄油刀)的基本使用
来源:互联网 发布:python的sin 编辑:程序博客网 时间:2024/04/30 02:49
出自:github,jake大神。附录上github地址https://github.com/JakeWharton/butterknife
用途的话:绑定视图控件而作用的。
使用准备:
使用android studio
添加依赖,File->Project Structure->Dependencies->Library dependency 搜索butterknife即可
使用Eclipse
添加jar包,网上都有
注意:1 使用as的可以使用插件,在AndroidStudio->File->Settings->Plugins->搜索Zelezny ,可以自动生成你需要的view对象,方便快捷。使用时,在要导入注解的Activity 或 Fragment 或 ViewHolder的layout资源代码上,右键——>Generate——Generate utterKnife Injections
2 使用ec的需要配置下,不然不会生效,设置方法,请看——>点击打开链接
基本使用
1 绑定view
class ExampleActivity extends Activity { @BindView(R.id.title) TextView title; @BindView(R.id.subtitle) TextView subtitle; @BindView(R.id.footer) TextView footer; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simple_activity); ButterKnife.bind(this); // TODO Use fields... }}
2 绑定资源文件
class ExampleActivity extends Activity { @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 // ...}3 fragment使用
public class FancyFragment extends Fragment { @BindView(R.id.button1) Button button1; @BindView(R.id.button2) Button button2; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fancy_fragment, container, false); ButterKnife.bind(this, view); // TODO Use fields... return view; }}4 适配器中使用
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); } }}5 将多个view放进list或数组中
@BindViews({ R.id.first_name, R.id.middle_name, R.id.last_name })List<EditText> nameViews;
ButterKnife.apply(nameViews, DISABLE);ButterKnife.apply(nameViews, ENABLED, false);6 监听事件的绑定,比如这里button的onclick属性,参数可写可不写。也可以限制泛型。
<Button android:id="@+id/btn1"android:layout_marginTop="10px"android:layout_width="match_parent"android:layout_height="70px"android:background="#0F0"android:onClick="submit"/>
@OnClick(R.id.btn1)void submit(){//...}
@OnClick(R.id.submit)void submit(View view) { // TODO submit data to server...}7 事件监听,限制泛型
@OnClick(R.id.submit)public void sayHi(Button button) { button.setText("Hello!");}8 解绑(一般fragment中用)
@Override public void onDestroyView() { super.onDestroyView(); unbinder.unbind(); }9 如listview的onItemClick方法
//原来监听方法,有啥写啥@OnItemClick(R.id.listview)void onItemClick(AdapterView<?> parent, View view,int position, long id) {//...}
开始对9不是很理解,不知道是写原先回调方法的重载都行,后来发现是写一样的,就是相当于把override下面的方法写下就行。其他也是一样,有多个override就写多个bind@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {// TODO Auto-generated method stub}这里注意下,因为不少view是继承textView的,所以如果响应时间没写限制,或者写了高级的类名,可能会同事响应多个view。比如,button 是textview的子类,放写的是textview的类别,可能点击button也会生效
@OnClick({R.id.tv1,R.id.tv2,R.id.btn2,R.id.cb1,R.id.cb2})void OnClick(TextView view){}
阅读全文
0 0
- Butter Knife(黄油刀)的基本使用
- 一把好用的黄油刀——Butter Knife
- Butter Knife框架的基本使用
- Butter Knife的使用
- Butter Knife 的使用
- Butter Knife的使用
- Butter Knife的使用
- Annotation 注解原理 @Butter Knife黄油刀进阶
- butter knife框架的使用
- Butter Knife的配置和使用
- 关于Butter Knife 的使用以及配置
- Butter Knife 的简介和使用
- Butter Knife使用教程
- Butter Knife使用笔记
- 使用Butter Knife
- Butter-Knife使用
- Butter Knife 详细使用
- Android Butter Knife使用
- 图像
- [李景山php] ddos 攻击 之 无法停止的毒药 p2p 网络攻击
- Android N Display Size
- revit 二次开发 如何修改元素的表面颜色
- Builder模式设置AlertDialog字体大小、颜色等属性
- Butter Knife(黄油刀)的基本使用
- LaTex数学公式学习
- Keil(MDK-ARM)系列教程(四)_工程目标选项配置(Ⅱ)
- Fiddler:手机端抓包配置
- AJAX 状态值(readyState)与状态码(status)详解
- Mybatis中的SQL的日期比较异常错误之一
- 开源优势
- PHP环境搭建(8)----nginx安装与配置
- Android代码内存优化建议-Android官方篇