package com.example.ele_me.util;

 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;
    }
//mActicity本身擁有獨立的變量,並賦值給class Injector,然而我們也可透過修改activity去改變mActivity。
    /**
     * 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()
//inject等待被另一個Java檔召喚。
{
        for (Field field : mActivity.getClass().getDeclaredFields())
//Field是一個對象類型,其作用等同於findViewById一樣,在於捕捉其對象。而下面的for迴圈則為了去捕捉對象所需要運用到的算式。
{
            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);
                    }
                }
            }
        }
    }
}

這是一個必須複製的編碼。我們必須重新開一個Java檔案去讓injectView運行,injectView本身並不會獨自地去尋找對象,而是透過injectView Inject()去運算,並且尋找對象。

安卓中級教程(2):@InjectView中的對象inject的更多相关文章

  1. 安卓中級教程(3):ScrollView

    以上是scrollview的圖例,可見srollview是一種滑動功能的控件,亦是非常常見的控件. 一般寫法如下: package com.mycompany.viewscroller; import ...

  2. 安卓中級教程(10):@InjectView

    package com.example.android.db01; import android.app.Activity; import android.content.ContentValues; ...

  3. 安卓中級教程(1):@InjectView

    package com.mycompany.hungry; import android.annotation.SuppressLint; import android.app.Activity; i ...

  4. 安卓中級教程(9):pathbutton中的animation.java研究(2)

    src/geniuz/myPathbutton/composerLayout.java package geniuz.myPathbutton; import com.nineoldandroids. ...

  5. 安卓中級教程(8):pathbutton中的animation.java研究(1)

    src/geniuz/myPathbutton/myAnimations.java package geniuz.myPathbutton; import java.util.ArrayList; i ...

  6. 安卓中級教程(6):annotation的基本用法

    package com.example.ele_me.activity; import android.annotation.SuppressLint; import android.app.Acti ...

  7. 安卓中級教程(5):ScrollView與refreshable之間的設置

    設置向下拉動更新. package com.mycompany.Scroll_test; import android.app.*; import android.os.*; import andro ...

  8. 安卓中級教程(4):ScrollView與ListView之間的高度問題

    在scrollView中加插ListView是一個大難題.其中一個難題是Listview的高度難以計算,輸出效果往往強差人意,就讓我們看看當中的問題 . <LinearLayout xmlns: ...

  9. 安卓中級教程(11):深入研究餓了麼的各個java檔運作關係(1)

    package com.example.ele_me.activity; import android.annotation.SuppressLint; import android.app.Acti ...

随机推荐

  1. DSP using MATLAB 示例Example3.4

    代码: n = [-1:3]; x = [1:5]; % x(n) = {1,2,3,4,5} % * % k = 0:500; w = (pi/500)*k; % [0,pi] axis divid ...

  2. redis 的使用 ( set集合类型操作)

    set 集合类型 释义:            redis 的 set 是 string 类型的无序集合 set 元素最大可以包含(2的32次方-1)个元素 关于 set 集合类型除了基本的添加删除操 ...

  3. spring mvc 入门配置

    1. 把所需jar拷贝到工程目录下WEB-INF/lib 2. 配置WEB.xml,配置前端控制器 org.springframework.web.servlet.DispatcherServlet ...

  4. 疯狂java笔记(七) - Java集合之Map

    Map是以键值对(key-value)的形式来存储数据的.而且Map不允许key的重复,通过Map存储key-value对时,只需要考虑key的存储就可以,key存储后value就会跟着key(完全可 ...

  5. 注册事件处理程序onclick和addEventListener、attachEvent

    一.设置HTML标签属性为事件处理程序(注意和下面的设置javascript对象属性为事件处理程序是不同的) 用于设置文档元素事件处理程序属性也能化成对应的HTML标签的属性.如果这样做,属性值应该是 ...

  6. JQuery validate验证 自定义

    http://www.w3cschool.cc/jquery/jquery-plugin-validate.html http://blog.163.com/zhao_jinggui/blog/sta ...

  7. ural 2069. Hard Rock

    2069. Hard Rock Time limit: 1.0 secondMemory limit: 64 MB Ilya is a frontman of the most famous rock ...

  8. Android开发环境变量配置总结

    每次重装系统或者拿到一台新电脑都得重配Android的环境变量,每次都得上网查,索性就记在这儿吧. “系统属性->高级系统设置->环境变量” 打开环境变量配置窗口. JDK配置: 新建JA ...

  9. BZOJ3072 : [Pa2012]Two Cakes

    考虑DP,设$f[i][j]$表示考虑了$a[1..i]$和$b[1..j]$的最小代价. 若$a[i]==b[j]$,则$f[i][j]=\min(f[i-1][j],f[i][j-1])+1$. ...

  10. 据说最近IMO中国队失利的一题

    (图基于Microsoft PaintBrush技术构建) 平面几何是可以难得出蛆的.这道题难在多圆.高度非对称和具有一定复杂性.如图,对ABC,H是垂心,O是垂足,M是中点.QK在ABC外接圆上,均 ...