Android简单的View注入
来源:互联网 发布:店铺形象设计淘宝 编辑:程序博客网 时间:2024/04/30 19:06
本来是查找Android sdk samples里面有哪些使用annotation的例子,无意中在VoicemailProviderDemo中发现了这个View的注入实现
像其他的注入框架一样先声明一下
// Mark the views as injectable. These objects are instantiated automatically during // onCreate() by finding the appropriate view that matches the specified resource_id. @InjectView(R.id.start_recording_btn) private Button mStartRec; @InjectView(R.id.save_btn) private Button mSaveButton;
然后在onCreate中调用
Injector.get(this).inject();然后看下InjectView
/** * 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();}
也跟其他自定义注解的方式一样
最后贴一下完整的Injector类
/* * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.example.android.voicemail.common.inject;import android.app.Activity;import java.lang.annotation.Annotation;import java.lang.reflect.Field;/** * 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();//获取注解中的vaule 既@InjectView后面括号中的值,cast这种强转方式只是看起来更优雅些 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); } } } } }}
0 0
- Android简单的View注入
- ButterKnifeZelezny简单实用的view注入插件
- Android自定义简单的View
- Android自定义简单的View
- Android自定义简单的View
- Java /android 依赖注入的简单实现
- xUtils之view注入--Android
- Android butterknife view注入框架
- Android 编译时View注入工具的实现
- View注入框架:Butterknife简单使用
- Android之---ButterKnife-View注入框架(简单介绍和在Studio中安装)
- Android自定义View 一<最简单的自定义View>
- Android的简单图形和view刷新
- Android简单的广告控件View
- Android的View 简单理解和实例
- android简单的九宫格解锁view
- Android的View 简单理解和实例
- 简单举例说明android自定义view的方法
- Js--ReactJs--NodeJs
- IAR uCOS .icf栈、堆区大小设置规则
- 3Sum | LeetCode O(N*N) Solution
- 从入门到精通之专家系统CLIPS(一)CLIPS初识与概述
- 黑马程序员--面向对象(day04)
- Android简单的View注入
- 抽象工厂模式--HeadFirst学习
- 如何关掉visual studio 2012 实时调试器
- 思想汇报(三)——永恒的爱情
- 【openjudge】Minecraft
- 以老K日记为例浅谈如何打造有固定粉丝的网站策略
- GUI对话框
- LeetCode Triangle
- hibernate的查询缓存