BindView注解类@Target(ElementType.FIELD)//表示要注解的是一个字段@Retention(RetentionPolicy.RUNTIME)//添加@interface表明这是一个注解,等价于继承了java.lang.annotation.Annotation这个类public @interface BindView {    public int id();//viewid    public boolean clickable() default false;//是否可点击,默认为false}使用
public class AnnotationActivity extends Activity implements View.OnClickListener {    @BindView(id = R.id.text_anno, clickable = true)    private TextView textView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_annotation);        initView(this, findViewById(android.R.id.content));    }    private void initView(Object viewClass, View view) {        Field[] fields = viewClass.getClass().getDeclaredFields();        if (fields != null && fields.length > 0) {            for (Field field : fields) {                BindView bindView = field.getAnnotation(BindView.class);                if (bindView != null) {                    int id = bindView.id();                    boolean clickable = bindView.clickable();                    try {                        field.setAccessible(true);                        if (clickable) {                            view.findViewById(id).setOnClickListener((View.OnClickListener) viewClass);                        }                        //viewClass中的field赋值为view.findViewById(id)                        field.set(viewClass, view.findViewById(id));                    } catch (IllegalAccessException e) {                        e.printStackTrace();                    }                }            }        }    }    @Override    public void onClick(View v) {        Toast.makeText(this, "click!", Toast.LENGTH_SHORT).show();        textView.setText("click");    }

更多相关文章

  1. android sqlite查询数据表的字段与相关属性
  2. android通过自定义toast实现悬浮通知效果的示例代码
  3. android fragment用get/setArguments()传值
  4. Android(安卓)Kotlin入门-属性和字段
  5. Android(安卓)Gson 操作
  6. Android短信操作(通过内容提供者)
  7. Android与JS互相调用以及注意
  8. Android(安卓)Afianl框架(2)——FinalDB
  9. Android(安卓)自定义控件,模仿小米秒表样式的时钟,完整代码注解

随机推荐

  1. Android(安卓)Studio 1.1 使用介绍及导入
  2. 进程间通过intent传递数据失败
  3. Android四方形输入框、密码框
  4. 获取手机屏幕大小(DisplayMetrics类取得
  5. Android(安卓)动画学习
  6. android busybox解决adbshell命令不全
  7. Android(安卓)Service 通知Activity更新
  8. android activity之间共享数据(applicatio
  9. Rxjava入门
  10. Google VR for Android(安卓)- Getting S