Android应用中使用运行时注解

来源:互联网 发布:淘宝助理教学 编辑:程序博客网 时间:2024/06/07 03:22

其实非常简单,直接上代码:本文主要是替代传统的findViewById()的功能,就是在我们Activity中不需要再使用findViewById()去给View赋值了,通过注解在运行阶段自动赋值。以及setOnClickListener()也是一样的原理。使用注解和反射技术。


1. 定义自己的annotation注解。

     定义findViewbyId这个功能的注解

package com.xxx.app.inject;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target({ ElementType.FIELD })@Retention(RetentionPolicy.RUNTIME)public @interface InjectView {   int value() default (int) -1;}

      定义setOnclickListener的注解

package com.xxx.app.inject;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target({ ElementType.METHOD })@Retention(RetentionPolicy.RUNTIME)public @interface InjectClick {   int[] value();}
2. 定义自己的注解处理类:


package com.xxx.app.inject;import android.app.Activity;import android.view.View;import java.lang.annotation.Annotation;import java.lang.reflect.Field;import java.lang.reflect.Method;public class Injector {   public static void injectView(Object obj, Object root) {      Field[] fields = obj.getClass().getDeclaredFields();      for (Field field : fields) {         field.setAccessible(true);         Annotation[] annotations = field.getAnnotations();         if (annotations != null) {            for (Annotation annotation : annotations) {               if (annotation instanceof InjectView) {                  InjectView injectView = (InjectView) annotation;                  int value = injectView.value();                  if (value != -1) {                     try {                        View view = getViewByRoot(root, value);                        field.set(obj, view);                     } catch (IllegalArgumentException e) {                        e.printStackTrace();                     } catch (IllegalAccessException e) {                        e.printStackTrace();                     }                  }                  break;               }            }         }      }   }   public static void injectClick(Object obj, Object root) {      Method[] methods = obj.getClass().getDeclaredMethods();      for (Method method : methods) {         Annotation[] annotations = method.getAnnotations();         if (annotations != null) {            for (Annotation annotation : annotations) {               if (annotation instanceof InjectClick) {                  InjectClick inject = (InjectClick) annotation;                  int[] value = inject.value();                  if (value != null && value.length > 0) {                     View.OnClickListener listener = (View.OnClickListener) obj;                     try {                        for (int res : value) {                           View view = getViewByRoot(root, res);                           if (view == null) {                              throw new NullPointerException();                           }                           view.setOnClickListener(listener);                        }                     } catch (IllegalArgumentException e) {                        e.printStackTrace();                     }                  }               } else if (annotation instanceof InjectLongClick) {                  InjectLongClick inject = (InjectLongClick) annotation;                  int[] value = inject.value();                  if (value != null && value.length > 0) {                     View.OnLongClickListener listener = (View.OnLongClickListener) obj;                     try {                        for (int res : value) {                           View view = getViewByRoot(root, res);                           if (view == null) {                              throw new NullPointerException();                           }                           view.setOnLongClickListener(listener);                        }                     } catch (IllegalArgumentException e) {                        e.printStackTrace();                     }                  }               }            }         }      }   }   public static View getViewByRoot(Object root, int res) {      View view = null;      if (root instanceof Activity) {         view = ((Activity)root).findViewById(res);      }       return view;   }}

3. Activity中使用注解:

@InjectView(R.id.action_back)private ImageView actionBack;@InjectView(R.id.site_top_bg)private ImageView mSiteTopBg;@InjectView(R.id.site_name)private TextView mSiteName;@InjectView(R.id.site_producter)private TextView mProducter;@InjectView(R.id.site_logo)private ImageRoundView mSiteLogo;@InjectView(R.id.site_description)private TextView mSiteDescription;@InjectView(R.id.viewPager)private ViewPager mViewPager;

@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_site);    Injector.injectView(this, this);    Injector.injectClick(this, this);    }
@Override@InjectClick({R.id.action_back})public void onClick(View v) {    int id = v.getId();    switch (id){        case R.id.action_back:            SiteDetailActivity.this.finish();            break;    }}

0 1
原创粉丝点击