在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Android/ Inject View
Just-in-time Bindings
Binding Annotations
Standard Injection
第一個例子 Hello World
Bindings 概述
Linked Bindings
如何綁定 generic 類型
@Provides Methods
RoboGuice 功能描述
概述
綜合示例 Astroboy
Inject Resources
Instance Bindings
Inject 自定義 View
Scopes
Provider Bindings
Untargetted Bindings
Inject Extra
第一個例子 Hello World
Inject Context
發(fā)送接收 Events
Inject View

Inject View

在例子 Android RoboGuice 使用指南(2):第一個例子 Hello World 介紹了使用 Roboguice 開發(fā)的基本步驟:

  1. 創(chuàng)建一個 RoboApplication 的子類 GuiceApplication,GuiceApplication 為 Appliacation 的子類,修改 AndroidManifest.xml,將 Application 的 name 指向這個類。
  2. 將原先由 Activity 派生的類基類改為 RoboActivity(或其它相關 Activity).
  3. 如果有需要的話在 AbstractAndroidModule 中重載 configuatation 方法定義 bindings.

如果不使用 Roboguice,如果 Activity 中需要訪問定義在 Layout 中的某個 View,一般需要使用 findViewById 來查找對應的 View,并將它強制轉換為對應的類,如果需要訪問的 View 很多,重復的代碼就非常繁瑣。

如果使用 Roboguice 的 Inject View ,代碼就簡潔易讀多了,@Inject View 的基本用法如下:

@InjectView (R.id.xxx) ViewType viewInstance;

  • R.id.xxx 為所需 View 定義在 Layout 中的 id ,如 R.id.textview1
  • ViewType 為所需 View 的類型,如 TextView
  • viewInstance 為變量名。

我們定義一個 injectview.xml ,如下:


<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”match_parent”
android:layout_height=”match_parent”>

<TextView 
android:id=”@+id/textview1″
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”@string/injectview”
/>

<TextView 
android:id=”@+id/textview2″
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”@string/injectview”
/>

<TextView 
android:id=”@+id/textview3″
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”@string/injectview”
/>

<TextView 
android:id=”@+id/textview4″
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”@string/injectview”
/>

<Button android:id=”@+id/button”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_gravity=”center_vertical”
android:text=”@string/clickmebutton”/>

</LinearLayout>

定義了4個 TextView 和一個 Button,看看如何使用 InjectView 來訪問這些 View:

public class InjectViewDemo extends RoboActivity {

 @InjectView (R.id.button) Button goButton;
 @InjectView (R.id.textview1) TextView textView1;
 @InjectView (R.id.textview2) TextView textView2;
 @InjectView (R.id.textview3) TextView textView3;
 @InjectView (R.id.textview4) TextView textView4;

 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

 setContentView(R.layout.injectview);
 goButton.setOnClickListener(mGoListener);
 }

 private OnClickListener mGoListener = new OnClickListener()
 {
 public void onClick(View v)
 {
 textView1.setText("Clicked");
 textView2.setText("Clicked");
 textView3.setText("Clicked");
 textView4.setText("Clicked");
 }
 };
}

無需使用 findViewById 來為每個變量(如 textview1)賦值,只需使用 @InjectView 標記,賦值的工作都由 Roboguice 來完成,程序只需向 Roboguice 說“給我某個 View”,Roboguice 就通過 Dependency Injection 傳給應用程序所需 View 的實例對象。代碼比不使用 Roboguice 時簡潔多了。

http://wiki.jikexueyuan.com/project/android-roboguice/images/15.png" alt="" />