注解式框架的使用将会大大简化代码编写量,提升开发效率,主流的注解式框架有Dagger,ButterKnife,AndrodAnnotations。AndrodAnnotations配置麻烦,需要在项目清单里注册生成的子类。反射机制会占用资源内存和耗时。Dagger采用预编译技术,高效,但是对View绑定操作注解不是很方便。
        ButterKnife用起来方便,使用简单,这里主要介绍它的使用。 到官网下载jar包,ButterKnife也已经托管到github上,http://jakewharton.github.io/butterknife/
      一.配置:
下载jar包放到libs中,配置eclipse,右键项目properties->java Compiler->Annotation Procession->勾一下Enable project special settings其他的就会自动勾上了,Factory path勾上Enableproject special settings,Add jars选中刚才导入的jar包,确定即可。
 二.使用:
    直接上代码<br />
    <strong>public</strong> <strong>class</strong> MainActivity <strong>extends</strong> Activity {<br />
@Optional@InjectView(R.id.tv)
Button btn;
@Override
<strong>protected</strong> <strong>void</strong> onCreate(Bundle savedInstanceState) {
    <strong>super</strong>.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.inject(<strong>this</strong>);}@OnClick(R.id.tv)
<br />
<strong>public</strong> <strong>void</strong> say(Button btn){
    Toast.makeText(<strong>this</strong>, "你好", Toast.LENGTH_SHORT).show();
    btn.setText("ButterKnife");
}}
很简单吧,需要注意的是,如果click事件还有其他的组件,则声明为@OnClick({R.id.tv,R.id.tv2})这种方式即可,在使用case来进行判断,@Optional是以防没有这个id,防止崩溃使用的,其他的事件如长按,选中等也都有相关的方法。
   三.对adapter的支持
之前我们自定义的adapter会写成这种形式:
    @Override<br />
<strong>public</strong> View getView(<strong>int</strong> position, View convertView, ViewGroup parent) {
    // <strong>TODO</strong> Auto-generated method stub
    ViewHolder holder = <strong>null</strong>;
    <strong>if</strong> (holder == <strong>null</strong>) {
        convertView = LayoutInflater.from(mcontext).inflate(
                R.layout.activity_main, <strong>null</strong>);
        holder = <strong>new</strong> ViewHolder();
        holder.btn = (Button) convertView.findViewById(R.id.tv);
        convertView.setTag(holder);
    } <strong>else</strong> {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.btn.setText("更改");
    <strong>return</strong> convertView;
}
<strong>class</strong> ViewHolder {
    Button btn;
}----------------------华丽丽的分割线-------------------------------
加入ButterKnife后,adapter只需进行很小的修改即可,代码如下
@Override
<strong>public</strong> View getView(<strong>int</strong> position, View convertView, ViewGroup parent) {
    // <strong>TODO</strong> Auto-generated method stub
    ViewHolder holder = <strong>null</strong>;
    <strong>if</strong> (holder == <strong>null</strong>) {
        convertView = LayoutInflater.from(mcontext).inflate(
                R.layout.activity_main, <strong>null</strong>);
        holder = <strong>new</strong> ViewHolder(convertView);
        holder.btn = (Button)convertView.findViewById(R.id.tv);
        convertView.setTag(holder);
    }<strong>else{</strong>
        holder = (ViewHolder)convertView.getTag();
    }
    holder.btn.setText("更改");
    <strong>return</strong> convertView;
}
<strong>static</strong> <strong>class</strong> ViewHolder{
    @InjectView(R.id.tv)
    Button btn;
    <strong>public</strong> ViewHolder(View view){
        ButterKnife.inject(view);
    }
} ViewHolder声明为了静态的,holder = new ViewHolder中传入了 convertView。以上就是简单的ButterKnife的使用方法
        <p>版权声明:本文为博主原创文章,未经博主允许不得转载。</p>