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. 分享Kali Linux 2016.2第49周虚拟机

    分享Kali Linux 2016.2第49周虚拟机该虚拟机使用Kali Linux 2016.2第49周的64位镜像安装而成.基本配置如下:(1)该系统默认设置单CPU双核,内存为2GB,硬盘为50 ...

  2. [工作中的设计模式]建造者模式builder

    一.模式解析 建造模式是将复杂的内部创建封装在内部,对于外部调用的人来说,只需要传入建造者和建造工具,对于内部是如何建造成成品的,调用者无需关心. 以上是对建造者模式的官方定义,简单说就是对于复杂对象 ...

  3. Delphi Virtual String Tree 基本用法

    首先建立树节点所用的记录类型 type HXL_TNode = record name:string; desc:string; BRoot:Boolean; 定义该类型指针 pHXL_TNode = ...

  4. TStringList 常用操作

    //TStringList 常用方法与属性: var   List: TStringList;   i: Integer; begin   List := TStringList.Create;   ...

  5. jquery笔记(遍历)

    祖先: $("selector").parent().css():返回被选元素的直接父元素,并调用css方法. $("selector").parents(). ...

  6. CodeForceS#276-B(求最大值)

    B. Valuable Resources   Many computer strategy games require building cities, recruiting army, conqu ...

  7. myeclipse下拷贝的项目,tomcat下部署名称和导出为war包的名称默认值修改

    拷贝一个项目,作为一个新的项目,给它换了名字,这时候默认的部署名称等都是原来项目的,这时候要在属性里面修改一下.

  8. BZOJ2934 : [Poi1999]祭坛问题

    对于每个祭坛,算出每条线段阻碍它的角度区间,然后排序求并看看是否有空位即可,时间复杂度$O(n^2\log n)$. 这题在Main上官方时限是0.2S,因此需要几个常数优化: $1.$为了避免用at ...

  9. Storm与Spark:谁才是我们的实时处理利器

    Storm与Spark:谁才是我们的实时处理利器 ——实时商务智能目前已经逐步迈入主流,而Storm与Spark开源项目的支持无疑在其中起到了显著的推动作用.那么问题来了:实时处理到底哪家强? 实时商 ...

  10. 【BZOJ】2456: mode

    http://www.lydsy.com/JudgeOnline/problem.php?id=2456 题意:给一个$n<=500000$的数列,求出现次数超过$\lfloor \frac{n ...