官方例子里的小玩意。。。。。
一个注解:InjectView
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 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();
}
一个通过反射注解 并 实例化的功能类:
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);
}
}
}
}
}
}
使用时类似:
@InjectView(R.id.gygallery)
private Gallery gallery;
@InjectView(R.id.is_switcher)
private ImageSwitcher imageSwitcher;
Activity>onCreate(){
Injector.get(this).inject();//init views
}
分享到:
相关推荐
简单实现Android的findviewbyid IOC框架
Android注解省去findviewbyid,在Android开发的过程中有很多控件都要findviewbyid的,这样太麻烦了。
一个根据布局文件自动生成findViewById代码的android studio插件
自己用反射和注解写的一个android小框架 简单实用 不用再一个一个findViewById了 利用反射和注解只需要一句话初始化所有组件 强大
一个简单的注解实现 findViewById 和 setOnClickListener 的例子
Android开发中经常使用 findviewbyid来找控件, 这个工具类可以方便的生成代码,减少代码的书写。
Android技术知识点:如何使用视图绑定来消除findViewById()
【问题描述】 Android中如下代码: LinearLayout groupPollingAddress = (LinearLayout)findViewById(R.layout....AndroidGUI27中findViewById返回null的快速解决办法 – 玄机逸士的专栏 – 博客频道 – CSDN.NET
本篇文章纯属教大家怎么偷懒,那就是将xml文件和java文件相关联的findviewById 方法,正确的写法是:TextView=...1.1打开Androidstudio中的setting 1.2搜索插件并安装 我的是已经安装过的,所以会显示已安装
简单的注解类,代替android中的findViewById().
创建全新的视图将满足我们独特的UI需求。 本文介绍在指南针开发中会用到的罗盘的界面UI,通过继承View类实现的自定义视图,以此来深刻了解自定义视图。
Android Studio 插件 传播:heart: 关于 在GitHub上看到Android-Studio-Plugins 项目并有了想法,这里想做一份比较全面的Android Studio开发插件整理,希望可以帮助更多的 Android 开发者来加快开发速度,也希望...
下面小编就为大家带来一篇android getActivity.findViewById获取ListView 返回NULL的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
这是可以让你告别繁杂的findViewById的一个实例,通过注解的方式来实现。
为了减少频繁的调用findViewById(R.id...),可以采用一些注入框架,可以简化自己的代码,让你更专注于实际的功能开发,butterknife就是这样的一个框架.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,...
主要介绍了AndroidGUI27中findViewById返回null的快速解决办法的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
vq id R id imageView1 image android R drawable btn plus ; 自动转换成子类 textview imageView listView gridView EditText et vq id R id editText1 getView ; 第二次查找id的时候 没有从布局文件中去找 而是...
主要介绍了Android中findViewById获取控件返回为空问题怎么解决的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下