@InjectView的实例化
来源:互联网 发布:端口镜像配置 编辑:程序博客网 时间:2024/05/13 03:50
这里是一个官方例子,轻量级的处理,灵感来自于强大的开源项目RoboGuice,可以通过wiki文档了解。
https://github.com/roboguice/roboguice/wiki
基于反射的injectview:
Android的findViewById真是太烦人了,模板似的方法,要写在每个Activity,Fragment,Adapter里面。声明View和findView总是间隔着未知的行距;setOnClickListener之后,总是要寻找对应的onClick方法在何处。
难道Android就不能智能的把layout中的View相应的与对应field绑定起来?
答案是:Android本身不支持,但是我们可以通过一些hack达到目的。
这是提供一个实例化对象来实现:
package com.lsj.utils;import java.lang.annotation.Annotation;import java.lang.reflect.Field;import android.app.Activity;/** * Very lightweight form of injection, inspired by RoboGuice, for injecting common ui elements. * <p> * Usage is very simple. In your Activity, define some fields as follows: * * <pre class="code"> * @InjectView(R.id.fetch_button) * private Button mFetchButton; * @InjectView(R.id.submit_button) * private Button mSubmitButton; * @InjectView(R.id.main_view) * private TextView mTextView; * </pre> * <p> * Then, inside your Activity's onCreate() method, perform the injection like this: * * <pre class="code"> * setContentView(R.layout.main_layout); * Injector.get(this).inject(); * </pre> * <p> * See the {@link #inject()} method for full details of how it works. Note that the fields are * fetched and assigned at the time you call {@link #inject()}, consequently you should not do this * until after you've called the setContentView() method. */public final class Injector { private final Activity mActivity; private Injector(Activity activity) { mActivity = activity; } /** * Gets an {@link Injector} capable of injecting fields for the given Activity. */ public static Injector get(Activity activity) { return new Injector(activity); } /** * Injects all fields that are marked with the {@link InjectView} annotation. * <p> * For each field marked with the InjectView annotation, a call to * {@link Activity#findViewById(int)} will be made, passing in the resource id stored in the * value() method of the InjectView annotation as the int parameter, and the result of this call * will be assigned to the field. * * @throws IllegalStateException if injection fails, common causes being that you have used an * invalid id value, or you haven't called setContentView() on your Activity. */ public void inject() { for (Field field : mActivity.getClass().getDeclaredFields()) { for (Annotation annotation : field.getAnnotations()) { if (annotation.annotationType().equals(InjectView.class)) { try { Class<?> fieldType = field.getType(); int idValue = InjectView.class.cast(annotation).value(); field.setAccessible(true); Object injectedValue = fieldType.cast(mActivity.findViewById(idValue)); if (injectedValue == null) { throw new IllegalStateException("findViewById(" + idValue + ") gave null for " + field + ", can't inject"); } field.set(mActivity, injectedValue); field.setAccessible(false); } catch (IllegalAccessException e) { throw new IllegalStateException(e); } } } } }}
以上的injector,接下的是injectview
package com.lsj.utils;import java.lang.annotation.Retention;import java.lang.annotation.Target;import java.lang.annotation.ElementType; import java.lang.annotation.RetentionPolicy; /** * Use this annotation to mark the fields of your Activity as being injectable. * <p> * See the {@link Injector} class for more details of how this operates. */ @Target({ ElementType.FIELD }) @Retention(RetentionPolicy.RUNTIME) public @interface InjectView { /** * The resource id of the View to find and inject. */ public int value(); }
接着按照规定格式,你就可以放心地使用@injectview了。
用师兄的一句话说就是:省时省力,高效开发。
0 0
- @InjectView的实例化
- The usage of InjectView
- 使用InjectView和findViewById说拜拜
- 使用InjectView和findViewById说拜拜
- 使用InjectView和findViewById说拜拜
- android使用butterknife InjectView和BindView
- Spring_1_Bean的实例化实例
- 用实例工厂方法实例化SpringBean的完整实例
- 实例化的基本原理
- 模板的实例化
- 实例化的基本原理
- 模板的实例化
- 类的实例化
- 实例化的概念
- 变量的实例化
- 实例化的使用
- 实例化的使用
- LayoutInflater的实例化
- Android 图形:绘制渐变色奥运五环图形,游戏文字,验证码,Matrix旋转,缩放,倾斜,平移等
- Android ADB server didn't ACK * failed to start daemon * 简单有效的解决方案
- struts2页面到action中date类型自动转换
- Isomorphic Strings
- SVN(集中式源代码管理工具)
- @InjectView的实例化
- 数组指针操作一维二维数组
- 最新抓取淘宝价格数据,图片标题的就更好抓了整合就好
- ListView实现下拉刷新及上拉加载更多的一种方式
- 关于MongoDB最大连接数的查看与修改
- android studio sdk更新 (2015-8-5)
- Android编译过程详解(一)
- 南邮 OJ 1308 背包问题
- 在代码中引入Null Object模式