安卓中級教程(2):@InjectView中的對象inject
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的更多相关文章
- 安卓中級教程(3):ScrollView
以上是scrollview的圖例,可見srollview是一種滑動功能的控件,亦是非常常見的控件. 一般寫法如下: package com.mycompany.viewscroller; import ...
- 安卓中級教程(10):@InjectView
package com.example.android.db01; import android.app.Activity; import android.content.ContentValues; ...
- 安卓中級教程(1):@InjectView
package com.mycompany.hungry; import android.annotation.SuppressLint; import android.app.Activity; i ...
- 安卓中級教程(9):pathbutton中的animation.java研究(2)
src/geniuz/myPathbutton/composerLayout.java package geniuz.myPathbutton; import com.nineoldandroids. ...
- 安卓中級教程(8):pathbutton中的animation.java研究(1)
src/geniuz/myPathbutton/myAnimations.java package geniuz.myPathbutton; import java.util.ArrayList; i ...
- 安卓中級教程(6):annotation的基本用法
package com.example.ele_me.activity; import android.annotation.SuppressLint; import android.app.Acti ...
- 安卓中級教程(5):ScrollView與refreshable之間的設置
設置向下拉動更新. package com.mycompany.Scroll_test; import android.app.*; import android.os.*; import andro ...
- 安卓中級教程(4):ScrollView與ListView之間的高度問題
在scrollView中加插ListView是一個大難題.其中一個難題是Listview的高度難以計算,輸出效果往往強差人意,就讓我們看看當中的問題 . <LinearLayout xmlns: ...
- 安卓中級教程(11):深入研究餓了麼的各個java檔運作關係(1)
package com.example.ele_me.activity; import android.annotation.SuppressLint; import android.app.Acti ...
随机推荐
- SU Demos-07NMO
本例还不完善,不足之处,欢迎批评指正 先看readme 第1个脚本,显示速度模型 脚本#2,生成数据 脚本#3,显示合成数据 脚本#4,进行速度分析 脚本#5,动校叠加
- 使用wget
下载整个网站 需要下载某个目录下面的所有文件: wget -c -r -np -k -L -p url 有用到外部域名的图片或连接,如果需要同时下载就要用-H参数: wget -np -nH -r - ...
- js中各种宽高
各种宽高 Javascript: IE中:document.body.clientWidth ==> BODY对象宽度document.body.clientHeight ==> BODY ...
- TestNg依赖配置基础用法(单一方法依赖)------TestNg依赖详解(一)
TestNg依赖测试之简单方法依赖,通过dependsOnMethods属性来配置依赖方法 原创文章,版权所有,允许转载,标明出处:http://blog.csdn.net/wanghantong J ...
- App测试时,区分客户端或服务器端导致问题产生的方法
1.先确定产生问题的地方是否与服务器产生交互/通信,若无则非服务器问题: 2.通过Fiddler抓包,查看操作时调用的服务器接口是否正常并检查对应返回值: 3.若接口返回值正常,则需查看客户端对业务的 ...
- 斑点检测(LoG,DoG) [上]
斑点检测(LoG,DoG) [上] 维基百科,LoG,DoG,DoH 在计算机视觉中,斑点检测是指在数字图像中找出和周围区域特性不同的区域,这些特性包括光照或颜色等.一般图像中斑点区域的像素特性相似甚 ...
- 【原】iOS学习之Swift之语法2(精简版)
1.可选类型和强制解包(?和!) 1> 可选类型(?)和强制解包(!) 在swift中,可选类型(?) 其根源是一个 枚举型,里面有 None 和 Some 两种类型.其实所谓的 nil 就是 ...
- Cellular Automata编写历程
2016.10.14:完成大致框架编写,控制台下实现 取点方式:南北半球对称取点;同一半球同一经度相邻点之间弧长相等;同一纬度相邻点之间弧长相等;不同纬度的圆周长度不等 地图设定为球形 2016.10 ...
- ccc array
setInterval可以用来设置函数的执行频率 nodeList: { default:[], type:[cc.Node] } active 可以用来设置是否启用 cc.Class({ exten ...
- 20145308刘昊阳 《Java程序设计》第3周学习总结
20145308刘昊阳 <Java程序设计>第3周学习总结 教材学习内容总结 第四章 认识对象 要产生对象必须先定义类,类是对象的设计图,对象是类的实例 类是从少数实例推广到大量相似实例的 ...