Android 反射简化findViewById

From http://blog.csdn.net/jjwwmlp456/article/details/19039063


官方例子里的小玩意。。。。。

一个注解:InjectView

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. import java.lang.annotation.ElementType;  
  2. import java.lang.annotation.Retention;  
  3. import java.lang.annotation.RetentionPolicy;  
  4. import java.lang.annotation.Target;  
  5.   
  6. /** 
  7.  * Use this annotation to mark the fields of your Activity as being injectable. 
  8.  * <p> 
  9.  * See the {@link Injector} class for more details of how this operates. 
  10.  */  
  11. @Target({ ElementType.FIELD })  
  12. @Retention(RetentionPolicy.RUNTIME)  
  13. public @interface InjectView {  
  14.     /** 
  15.      * The resource id of the View to find and inject. 
  16.      */  
  17.     public int value();  
  18. }  

一个通过反射注解 并 实例化的功能类:

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. import java.lang.annotation.Annotation;  
  2. import java.lang.reflect.Field;  
  3.   
  4. import android.app.Activity;  
  5.   
  6. /** 
  7.  * Very lightweight form of injection, inspired by RoboGuice, for injecting common ui elements. 
  8.  * <p> 
  9.  * Usage is very simple. In your Activity, define some fields as follows: 
  10.  * 
  11.  * <pre class="code"> 
  12.  * @InjectView(R.id.fetch_button) 
  13.  * private Button mFetchButton; 
  14.  * @InjectView(R.id.submit_button) 
  15.  * private Button mSubmitButton; 
  16.  * @InjectView(R.id.main_view) 
  17.  * private TextView mTextView; 
  18.  * </pre> 
  19.  * <p> 
  20.  * Then, inside your Activity's onCreate() method, perform the injection like this: 
  21.  * 
  22.  * <pre class="code"> 
  23.  * setContentView(R.layout.main_layout); 
  24.  * Injector.get(this).inject(); 
  25.  * </pre> 
  26.  * <p> 
  27.  * See the {@link #inject()} method for full details of how it works. Note that the fields are 
  28.  * fetched and assigned at the time you call {@link #inject()}, consequently you should not do this 
  29.  * until after you've called the setContentView() method. 
  30.  */  
  31. public final class Injector {  
  32.     private final Activity mActivity;  
  33.   
  34.     private Injector(Activity activity) {  
  35.         mActivity = activity;  
  36.     }  
  37.   
  38.     /** 
  39.      * Gets an {@link Injector} capable of injecting fields for the given Activity. 
  40.      */  
  41.     public static Injector get(Activity activity) {  
  42.         return new Injector(activity);  
  43.     }  
  44.   
  45.     /** 
  46.      * Injects all fields that are marked with the {@link InjectView} annotation. 
  47.      * <p> 
  48.      * For each field marked with the InjectView annotation, a call to 
  49.      * {@link Activity#findViewById(int)} will be made, passing in the resource id stored in the 
  50.      * value() method of the InjectView annotation as the int parameter, and the result of this call 
  51.      * will be assigned to the field. 
  52.      * 
  53.      * @throws IllegalStateException if injection fails, common causes being that you have used an 
  54.      *             invalid id value, or you haven't called setContentView() on your Activity. 
  55.      */  
  56.     public void inject() {  
  57.         for (Field field : mActivity.getClass().getDeclaredFields()) {  
  58.             for (Annotation annotation : field.getAnnotations()) {  
  59.                 if (annotation.annotationType().equals(InjectView.class)) {  
  60.                     try {  
  61.                         Class<?> fieldType = field.getType();  
  62.                         int idValue = InjectView.class.cast(annotation).value();  
  63.                         field.setAccessible(true);  
  64.                         Object injectedValue = fieldType.cast(mActivity.findViewById(idValue));  
  65.                         if (injectedValue == null) {  
  66.                             throw new IllegalStateException("findViewById(" + idValue  
  67.                                     + ") gave null for " +  
  68.                                     field + ", can't inject");  
  69.                         }  
  70.                         field.set(mActivity, injectedValue);  
  71.                         field.setAccessible(false);  
  72.                     } catch (IllegalAccessException e) {  
  73.                         throw new IllegalStateException(e);  
  74.                     }  
  75.                 }  
  76.             }  
  77.         }  
  78.     }  
  79. }  

使用时类似:
[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. @InjectView(R.id.gygallery)  
  2. private Gallery gallery;  
  3. @InjectView(R.id.is_switcher)  
  4. private ImageSwitcher imageSwitcher;  

Activity>onCreate(){

Injector.get(this).inject();//init views

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值