您的位置:首页 > 移动开发 > Android开发

Android中的注解式框架之ButterKnife的使用

2017-01-02 18:01 519 查看
我们常用的Android开发的模式基本上都是这样的,首先在xml布局中编写需要的各种控件,然后为这些控件添加id,之后再到代码里面去各种findViewbyId进行绑定控件,最后才能在这些控件上面进行具体的操作。这样开发项目没有任何的问题,但是如果一个xml布局中的用到的View非常多的时候呢?这样就难以避免一个问题,就是我们会增加很大的代码量,进行很多的无意义的工作,为什么说是无意义的,因为这些代码并不会提升我们的技术含量,写十遍和写一遍的效果是一样的,所以,在我们Android中出现了一种可以简化代码,提升开发效率,使代码结构清晰简洁的框架——注解式框架。

首先我们来了解一下什么是注解式开发?

这是在JDK1.5之后推出的一种方式,我们在Java或者Android开发的过程中,如果想要使用注解式开发,我们就要自定义注解。
基本结构是:@ + 注解名(也可以叫类名)+ 传递的属性值(这个属性值包括key和value,中间用“=”连接),可以设置目标范围:方法(Method)、属性(Field)、类(Type)
自定义注解要用到@interface:用于定义注解;@Target:用于描述注解的使用范围;@Retention:注解的生命周期,一般RetentionPolocy.RUNTIME
接着我们来看一下为什么要使用ButterKnife这个框架?

Android中比较常用的有这么几种Dagger、ButterKnife、AndroidAnnotations,AndroidAnnotations是一个基于注解方式来简化代码结构,提高开发效率的开源框架,但是它配置起来比较麻烦,需要在清单文件中去注册生成的子类,并且它的这种反射机制会占用资源内存和耗时,虽然影响并不大。Dagger采用的是预编译技术,高效,但是对View绑定操作注解不是很方便。所以今天跟大家分享的是ButterKnife,它用起来方便,配置也比较简单,有着强大的View注入绑定和简单的常用方法注解。
官网:http://jakewharton.github.io/butterknife/
最后我们来看一下ButterKnife的具体使用方法:
1.首先来看如何配置ButterKnife,我这里使用的是AndroidStudio开发,配置起来也比较简单:
在AndroidStudio->File->Project
Structure->app->Dependencies->"+"->Library dependency->搜索butterknife之后点击OK即可,等待AS为我们完成依赖的添加。

这个时候app的build.gradle文件的dependencies里面将会自动添加这样一句话:

compile 'com.jakewharton:butterknife:8.4.0'

此时表示已经添加了ButterKnife的依赖,但是这里需要注意的是,仅仅添加这一句依赖有可能会导致之后使用OnClick()方法的时候不起作用,8.4.0出现这个问题,本人亲测,其它版本不知道是否存在这个问题所以这里一并给出解决办法,还要添加一句依赖:

annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'

另外这里推荐大家使用一个Zelezny插件:在AndroidStudio->File->Settings->Plugins->搜索Zelezny下载添加就行
,可以快速生成对应组件的实例对象,不用手动写。使用时,在要导入注解的Activity 或 Fragment 或 ViewHolder的layout资源代码上,右键——>Generate——Generate ButterKnife Injections,然后会出现选择框进行选择后点击confirm即可。

2.配置完成之后我们就可以在项目中具体的使用了,先来看一下控件如何快速绑定的,在xml文件中添加两个文本控件,代码为:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<TextView
android:id="@+id/id_butterknife_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#f37477"
android:text="测试绑定控件" />

<TextView
android:id="@+id/id_butterknife_text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#f37477"
android:text="测试绑定控件2"
android:layout_margin="10dp"/>
</LinearLayout>
然后在Java代码中,首先在setContentView的后面为ButterKnife绑定Activity,具体代码为:

ButterKnife.bind(this);


然后绑定具体控件:

@BindView(R.id.id_butterknife_text)
TextView idButterknifeText;


点击事件的操作:

@OnClick(R.id.id_butterknife_text)
public void toastHello(TextView textView) {
Toast.makeText(this, "Hello Android", Toast.LENGTH_SHORT).show();
textView.setText("ButterKnife");
}


多个控件的点击事件可以这样写:

@OnClick({R.id.id_butterknife_text,R.id.id_butterknife_text2})
public void toastHello(View view) {
switch (view.getId()){
case R.id.id_butterknife_text:
Toast.makeText(this, "Hello Android", Toast.LENGTH_SHORT).show();
idButterknifeText.setText("ButterKnife");
break;
case R.id.id_butterknife_text2:
idButterknifeText2.setText("哈哈");
break;
}
}

给ListView的Adapter的ViewHolder使用ButterKnife:

static class ViewHolder{
@BindView(R.id.item)
TextView textView;
public ViewHolder(View view){
ButterKnife.bind(view);
}
}

getView()方法基本保持不变,只需要在给ViewHolder()实例化的时候传入一个convertView即可。

最后给出一个LIstView展示数据的简单案例源码:http://download.csdn.net/detail/jarchie520/9727123

如果分享当中有错误的地方,欢迎大家批评指正!希望和大家一起共同进步!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息