码迷,mamicode.com
首页 > 其他好文 > 详细

ButterKnife 8.5.1使用总结

时间:2017-03-27 20:25:00      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:annotation   注解   findviewbyid   butterknife   

   最近在做一个OA项目,其中有许多的界面都是表单类型的数据收集,没完没了的findViewById敲的我手都麻木了,于是找到ButterKnife框架。实践过后觉得确实好用,个人认为代码结构也更清晰易读了,使用总结如下。


ButterKnife 简介

   ButterKnife是一个专注于Android系统的View注入框架,可以减少大量的findViewById以及setOnClickListener代码。在7.0版本以后引入了注解处理器,取代了之前利用反射原理进行findViewById影响APP性能的实现方式,不再影响APP运行效率。

 

GitHub: https://github.com/JakeWharton/butterknife

官方网站: http://jakewharton.github.io/butterknife/

 

使用须知:

1.Activity中使用ButterKnife.bind(this);必须在setContentView();之后,父类中bind后,子类不需要再bind

2.Fragment中使用ButterKnife.bind(this, rootView); 根布局View对象,需要在onDestroyView中解绑

3.注解的属性不能用private或static修饰,否则会报错

4.Activity官方例子中没有解绑操作,而Fragment需要解绑

 

使用步骤:

1、添加依赖

    compile ‘com.jakewharton:butterknife:8.5.1‘

    annotationProcessor ‘com.jakewharton:butterknife-compiler:8.5.1‘

备注:只需要添加上述依赖即可使用。

只有当你在Library中使用ButterKnife时,才需要如下步骤:

1、在Project的build.gradle中添加:

classpath‘com.jakewharton:butterknife-gradle-plugin:8.5.1‘

2、在对应Library(Module)中添加:

apply plugin:‘com.jakewharton.butterknife‘

网上许多的使用文章都把上述两个步骤当做是必须的。非也!

(注意:To use ButterKnife in a library

 

实际项目

实际项目中一般都会有定义一个BaseActivity或BaseFragment,在基类中绑定,则在其所有的子类中都可以直接使用。需要注意的是,父类中需要在子类的setContentView调用完后再绑定。

 

使用完整范例

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.textview)
    TextView textview;
    @BindView(R.id.button)
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_new);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.textview, R.id.button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.textview:
                break;
            case R.id.button:
                break;
        }
    }
}

@BindView(R.id.textview)//声明并绑定变量
TextView textview;
//相当于:TextViewtextView = (TextView) findViewById(R.id.textView);
 
@OnClick(R.id.button)//给按钮绑定方法,可以不声明引用变量
public void onViewClicked() {
}
 
@BindViews({R.id.button1, R.id.button2, R.id.button3})
public List<Button> mList ;
 
@BindString(R.string.app_name) //绑定字符串
String str;
 
@BindArray(R.array.city ) //绑定数组
String[] arr ;

 

在fragment中使用(需要解绑)

private Unbinder unbinder;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_regist_create, container, false);
    unbinder = ButterKnife.bind(this, view);//需要加多一个加载了布局的View对象
    return view;
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    unbinder.unbind();//fragment需要解绑
}

 

再偷懒一点

Android Studio插件:Android Butterknife Zelezny

方便给整个布局文件一键生成注解。

使用方法:

    在setContentView(R.layout.activity_main)中对着activity_main右键,generate,Generate ButterKnife Injections,勾选需要生成注解的控件即可

技术分享

图片来自android-butterknife-zelezny


总结

一般我认为简化了findViewById和setOnClickListener之后,已经很方便了。并不会去使用ButterKnife中方方面面的功能,在多控件的界面例如提交表单数据时非常好用。这也就是“80/20”法则在生效吧!对于使用比较少的其他特性如解除绑定等,用到之时再尝试即可!

 

参考文档

[Android开发] ButterKnife8.5.1 使用方法教程总结

 

同类型框架参考

google/dagger

https://github.com/google/dagger

androidannotations

https://github.com/androidannotations/androidannotations

 

附录:

注解类型

* @BindViews

* @BindView

* @BindArray

* @BindBitmap

* @BindBool

* @BindColor

* @BindDimen

* @BindDrawable

* @BindFloat

* @BindInt

* @BindString

 

事件类型

* @OnClick

* @OnCheckedChanged

* @OnEditorAction

* @OnFocusChange

* @OnItemClick item

* @OnItemLongClick

* @OnItemSelected

* @OnLongClick

* @OnPageChange

* @OnTextChanged

* @OnTouch

* @Optional

 

===================================================================================

Github上的使用说明:

Download

dependencies {

  compile ‘com.jakewharton:butterknife:8.5.1‘

  annotationProcessor ‘com.jakewharton:butterknife-compiler:8.5.1‘

}

Snapshots of thedevelopment version are available in Sonatype‘s snapshots repository.


Library projects

To use ButterKnife in a library, add the plugin to your buildscript:

buildscript {

  repositories {

    mavenCentral()

   }

  dependencies {

    classpath ‘com.jakewharton:butterknife-gradle-plugin:8.5.1‘

  }

}

and then apply it in your module:

    apply plugin: ‘com.android.library‘

    apply plugin: ‘com.jakewharton.butterknife‘

Now make sureyou use R2 instead of R inside allButter Knife annotations.

classExampleActivityextendsActivity {

    @BindView(R2.id.user) EditText username;

    @BindView(R2.id.pass) EditText password;

...

}

==================================================================================

 


本文出自 “一剑围城” 博客,请务必保留此出处http://weijiancheng.blog.51cto.com/10190955/1910671

ButterKnife 8.5.1使用总结

标签:annotation   注解   findviewbyid   butterknife   

原文地址:http://weijiancheng.blog.51cto.com/10190955/1910671

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!