安卓中級教程(2):@InjectView中的對象inject

 1 package com.example.ele_me.util;
 2
 3 import java.lang.annotation.Annotation;
 4 import java.lang.reflect.Field;
 5
 6 import android.app.Activity;
 7
 8 /**
 9  * Very lightweight form of injection, inspired by RoboGuice, for injecting common ui elements.
10  * <p>
11  * Usage is very simple. In your Activity, define some fields as follows:
12  *
13  * <pre class="code">
14  * @InjectView(R.id.fetch_button)
15  * private Button mFetchButton;
16  * @InjectView(R.id.submit_button)
17  * private Button mSubmitButton;
18  * @InjectView(R.id.main_view)
19  * private TextView mTextView;
20  * </pre>
21  * <p>
22  * Then, inside your Activity‘s onCreate() method, perform the injection like this:
23  *
24  * <pre class="code">
25  * setContentView(R.layout.main_layout);
26  * Injector.get(this).inject();
27  * </pre>
28  * <p>
29  * See the {@link #inject()} method for full details of how it works. Note that the fields are
30  * fetched and assigned at the time you call {@link #inject()}, consequently you should not do this
31  * until after you‘ve called the setContentView() method.
32  */
33 public final class Injector {
34     private final Activity mActivity;
35
36     private Injector(Activity activity) {
37         mActivity = activity;
38     }
39 //mActicity本身擁有獨立的變量,並賦值給class Injector,然而我們也可透過修改activity去改變mActivity。
40     /**
41      * Gets an {@link Injector} capable of injecting fields for the given Activity.
42      */
43     public static Injector get(Activity activity) {
44         return new Injector(activity);
45     }
46
47     /**
48      * Injects all fields that are marked with the {@link InjectView} annotation.
49      * <p>
50      * For each field marked with the InjectView annotation, a call to
51      * {@link Activity#findViewById(int)} will be made, passing in the resource id stored in the
52      * value() method of the InjectView annotation as the int parameter, and the result of this call
53      * will be assigned to the field.
54      *
55      * @throws IllegalStateException if injection fails, common causes being that you have used an
56      *             invalid id value, or you haven‘t called setContentView() on your Activity.
57      */
58     public void inject()
59 //inject等待被另一個Java檔召喚。
60  {
61         for (Field field : mActivity.getClass().getDeclaredFields())
62 //Field是一個對象類型,其作用等同於findViewById一樣,在於捕捉其對象。而下面的for迴圈則為了去捕捉對象所需要運用到的算式。
63 {
64             for (Annotation annotation : field.getAnnotations()) {
65                 if (annotation.annotationType().equals(InjectView.class)) {
66                     try {
67                         Class<?> fieldType = field.getType();
68                         int idValue = InjectView.class.cast(annotation).value();
69                         field.setAccessible(true);
70                         Object injectedValue = fieldType.cast(mActivity.findViewById(idValue));
71                         if (injectedValue == null) {
72                             throw new IllegalStateException("findViewById(" + idValue
73                                     + ") gave null for " +
74                                     field + ", can‘t inject");
75                         }
76                         field.set(mActivity, injectedValue);
77                         field.setAccessible(false);
78                     } catch (IllegalAccessException e) {
79                         throw new IllegalStateException(e);
80                     }
81                 }
82             }
83         }
84     }
85 }

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

时间: 2024-08-21 23:04:22

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

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

1 package com.mycompany.hungry; 2 3 import android.annotation.SuppressLint; 4 import android.app.Activity; 5 import android.content.Intent; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import a

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

在scrollView中加插ListView是一個大難題.其中一個難題是Listview的高度難以計算,輸出效果往往強差人意,就讓我們看看當中的問題 . <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pa

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

src/geniuz/myPathbutton/myAnimations.java package geniuz.myPathbutton; import java.util.ArrayList; import java.util.List; import com.nineoldandroids.animation.Animator; import com.nineoldandroids.animation.Animator.AnimatorListener; import com.nineol

安卓中級教程(7):annotation中的 public @interface的用法

1 package com.example.ele_me.util; 2 3 import java.lang.annotation.Retention; 4 import java.lang.annotation.Target; 5 import java.lang.annotation.ElementType;  6 import java.lang.annotation.RetentionPolicy;  7 8 /** 9 * Use this annotation to mark th

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

1 package com.example.ele_me.activity; 2 3 import android.annotation.SuppressLint; 4 import android.app.Activity; 5 import android.os.Bundle; 6 import android.support.v4.widget.SlidingPaneLayout; 7 import android.view.View; 8 import android.view.Wind

安卓初級教程(3):ContentProvider的運用原理

1 package com.example.android.provider; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.Map; 6 import android.app.Activity; 7 import android.content.ContentValues; 8 import android.database.Cursor; 9 import android.net.

安卓初級教程(1):@Database(1)

1 package com.example.android.db01; 2 3 import android.app.Activity; 4 import android.content.ContentValues; 5 import android.content.Context; 6 import android.database.Cursor; 7 import android.database.sqlite.SQLiteDatabase; 8 import android.databas

安卓初級教程(5):TabHost的思考

1 package com.myhost; 2 3 import android.os.Bundle; 4 import android.view.LayoutInflater; 5 import android.widget.TabHost; 6 import android.app.TabActivity; 7 8 public class TabhostActivity extends TabActivity { // Extend TabActivity class 9 10 /** C

安卓初級教程(2):SD創建file,儲存與讀寫的方法(1)

package com.sdmadik; import java.io.*; import android.app.Activity; import android.os.Bundle; import android.view.*; import android.view.View.OnClickListener; import android.widget.*; public class FileUse extends Activity { // GUI controls EditText t