Android RoboGuice使用指南(2) 第一个例子Hello World

首先介绍一下如果将Guice 和RoboGuice 的库添加到项目中。

下载RoboGuice和guice-2.0-no_aop.jar(not guice-3.0),或者下载

创建一个新Android项目,比如GuiceDemo,目标平台Android1.5以上。

一般可以在该项目下添加一个lib目录,将两个jar文件拷到lib目录下,然后 通过: Project > Properties > Java Build Path > Libraries > Add External JARs

添加了对应guice 和roboguice库的引用之后,就可以开始编写第一个使用 roboguice 的例子。

使用roboguice 的步骤:

1. 创建一个 RoboApplication 的子类GuiceApplication,GuiceApplication为Appliacation 的子类,因此需要修改AndroidManifest.xml,将Application 的name 指向这个类 。可以参见Android简明开发教程九:创建应用程序框架

<application android:name=”GuiceApplication”

android:icon=”@drawable/icon” android:label=”@string/app_name”>

<activity android:name=”.GuiceDemo”

android:label=”@string/app_name” >

<intent-filter>

< action android:name=” android.intent.action.MAIN” />

<category android:name=” android.intent.category.LAUNCHER” />

< /intent- filter>

< /activity>

</application>

2. 在 这个简单的例子中,它使用的Layout 定义如下:

<?xml version=” 1.0″ encoding=”utf-8″?>

< LinearLayout xmlns:android=” http://schemas.android.com/apk/res/android”

android:orientation=” vertical”

android:layout_width=”fill_parent”

android:layout_height=”fill_parent”

>

<TextView

android:id=”@+id/hello”

android:layout_width=”fill_parent”

android:layout_height=” wrap_content”

android:text=”@string/hello”

/>

< /LinearLayout>

我们定义了一个TextView ,它的id为hello.

假定这个应用使用一个IGreetingService ,它有一个方法getGreeting() 返回一 个字符串,至于IGreetingService 如何实现,GuideDemo 不需要关心。

时间: 2024-09-14 16:27:20

Android RoboGuice使用指南(2) 第一个例子Hello World的相关文章

Android RoboGuice2使用指南(2) 第一个例子Hello World

本例实现的功能和例子Android RoboGuice 使用指南(2):第一个例子Hello World一样,所不同的是本例使用RoboGuice2.0 来实现. 下载新的RoboGuice库,Roboguice2.0 库有四个库组成,如下图所示: 库可以从 http://code.google.com/p/roboguice/下载. 2. 创建一个新Android项目,比如GuiceDemo,目标平台Android1.5以上. 3. 一般可以在该项目下添加一个libs目录,将两个jar文件拷到

Android RoboGuice使用指南(14) Inject View

在例子Android RoboGuice 使用指南(2):第一个例子Hello World 介绍了使用 Roboguice开发的基本步骤: 创建一个RoboApplication 的子类GuiceApplication,GuiceApplication为 Appliacation的子类,修改AndroidManifest.xml,将Application 的name 指向这 个类. 将原先由Activity派生的类基类改为RoboActivity(或其它相关 Activity). 如果有需要的话

Android RoboGuice使用指南(13) RoboGuice功能描述

前面在Android RoboGuice 使用指南(1):概述 对应Roboguice做了简要的介绍 ,之后介绍了Google Guice的基本用法,Roboguice是基本Android和Google Guice开发的适用于Android平台的Dependency Injection 开发包,下图为使用 Roboguice开发应用的基本框图: Android应用程序可以直接使用Google Guice来为普通类进行注入操作,而对 和Android平台相关的类如Activity,Context,

Android RoboGuice使用指南(15) Inject Context

在Android应用程序中,很多地方需要引用到Context对象(Activity, Application,Service等).Roboguice 使得引用Context对象变得非常容易. 可以参见下面例子,这里定义一个不在Activity中的类ContextInfo,需 要引用Context对象: class ContextInfo{ final Context context; @Inject ContextInfo(Context context){ this.context=conte

Android RoboGuice使用指南(6) Instance Bindings

我们在前面例子Android RoboGuice 使用指南(4):Linked Bindings 时为简单 起见,定义MyRectangle和MySquare时为它们定义了一个不带参数的构造函数,如 MyRectangle的如下: public class MyRectangle extends Rectangle{ public MyRectangle(){ super(50,50,100,120); } public MyRectangle(int width, int height){ s

Android RoboGuice使用指南(10) Just-in-time Bindings

Injector 通过检查bindings 定义来创建某个类型的实例对象.定义在Module 中的绑定称为"明确声明绑定(Explicit bindings".Injector 会首先使用带 有Explicit Bindings为某个类型创建实例对象. 当但某个类型没有明确定义绑 定时,Injector 试图构造"即时绑定(Just-in-time Bindings),JIT Bindings 也成为隐含绑定(implicit bindings). Eligible Cons

Android RoboGuice使用指南(5) Binding Annotations

有些情况需要将同一类型映射到不同的类实现,还是使用绘图的例 子. IShape, Rectangle, MyRectangle, MySquare,有如下继承关系: 我们可能需要将IShape 同时映射到 MyRectangle 和MySquare ,这时可以使用Binding Annotation 来实现. 这时使 用类型和annotation (标注)可以唯一确定一个Binding.Type 和annotation 对 称为Key(键). 为了同时使用MyRectangle和MySequar

Android RoboGuice使用指南(19) 发送接收Events

Roboguice 提供了对Context 生命周期相关的事件的send 和receive ,系统缺 省支持的事件为: OnActivityResultEvent OnConfigurationChangedEvent OnContentC hangedEvent OnContentViewAvailableEvent OnCreateEvent OnDestroyEv ent OnNewIntentEvent OnPauseEvent OnRestartEvent OnResumeEvent<

Android RoboGuice使用指南(18) Inject Resources

Roboguice 对访问res 目录下各种资源drawable, arrary, string 等也提供 了注入支持.可以通过@InjectResource 很方便的应用所需资源. 本例修 改Android ApiDemos示例解析(48):Content->Resources->Resources 使 用Inject Resource方法来访问资源. public class InjectResourceDemo extends RoboActivity { @InjectView (R.