Android(安卓)反射简化findViewById
16lz
2021-12-04
官方例子里的小玩意。。。。。
一个注解: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(安卓)常见知识整理(1)
- 如何打造一个 Android(安卓)编译时注解框架
- android Studio中关于Gradle的使用注解
- Android(安卓)编译时View注入工具的实现
- android studio使用android annotations注解
- AbstractProcessor注解处理器
- android 反射解析xml文件成为java对象
- Android中的注解
- Android(安卓)get property的一种方法