Butter Knife框架(小刀注解)_@BindView()用法

253 阅读2分钟

对于ButterKnife类官方的解释是:

Field and method binding for Android views. Use this class to simplify finding views and attaching listeners by binding them with annotations.

翻译过来就是:

Android视图的字段和方法绑定。使用此类通过将视图与注释绑定来简化查找视图和附加侦听器。


原生的获取组件是这样的:

public class MainActivity extends AppCompatActivity {
 
    //定义组件
    private TextView main_tv;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        main_tv = findViewById(R.id.main_tv);//获取组件
        main_tv.setText("111");//使用组件
    }
}

而使用 Butter Knife后的获取组件是这样子的:

public class MainActivity extends AppCompatActivity {
 
    @BindView(R.id.main_tv)  
    private TextView main_tv;//定义并获取组件
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);   //绑定activity
        main_tv.setText("111");//使用组件
    }
}

Butter Knife(小刀注解) 优点:

1、View绑定和Click事件处理功能,简化代码2、方便处理Adapter里的ViewHolder绑定问题3、运行时不会影响APP效率,使用配置方便

而今天遇到了一个在UI控件的上方使用该注解的情况。

注解的控件变量必须是public,否则报错。

@BindView(R.id.tracking_locus_btn)
    ImageButton mLocusBtn;

那这个注解有什么用呢?

代替了findViewById方法。也就是说你不用再去每个控件都去find一遍了。

点开这个注解你会发现~

import android.support.annotation.IdRes;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.CLASS;

/**
 * Bind a field to the view for the specified ID. The view will automatically be cast to the field
 * type.
 * <pre><code>
 * {@literal @}BindView(R.id.title) TextView title;
 * </code></pre>
 */
@Retention(CLASS) @Target(FIELD)
public @interface BindView {
  /** View ID to which the field will be bound. */
  @IdRes int value();
}

然后再创建一个注解处理器,当然只求会用的话,下面这个你也可以不看。

package com.example.zhujie;
import android.app.Activity;
import android.view.View;
import java.lang.reflect.Field;
//创建注解处理器
public class InjectUtils {
    public static void inject(Activity activity) throws IllegalAccessException {
        Field[] fields = activity.getClass().getDeclaredFields();
        for (Field field : fields) {
            BindView bindView = field.getAnnotation(BindView.class);
            if (bindView != null) {
                int value = bindView.value();
                View view = activity.findViewById(value);
                try {
                    field.set(activity, view);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

ButterKnife.bind(this);

参数就是传入当前页面的Activity。

然后使用set方法调整UI控件即可。


但是!

你如果在java代码中绑定了一个控件

但是你却没有使用,界面就会崩溃。


还有!

如果你在onClick方法想要用点击事件,那么也要在onClick方法上面加一个注解,注解的内容就是控件的ID。

 @OnClick({R.id.tracking_car_btn, R.id.tracking_mobile_btn})
    public void onClick(View v) {}

这个大佬写的很不错

t.csdn.cn/JkHG9