前面介绍了Android RoboGuice2 的HelloWorld示例,并介绍了从RoboGuice 1.1 升级到RoboGuice2.0 的主要注意事项。其它的基本概念和RoboGuice1.1基本一样,可以参见
本例介绍如何Inject自定义的View,Inject自定义的View和Android自带的View(如TextView,Button)方法一样。
本例使用一个自定义的TextView,每隔1秒显示当前时间。前定义如下:
- //--------------------------------- PACKAGE ------------------------------------
- package com.pstreets.guice.customview;
- //--------------------------------- IMPORTS ------------------------------------
- import android.content.Context;
- import android.os.Handler;
- import android.util.AttributeSet;
- import android.widget.TextView;
- import java.util.Calendar;
- import java.util.Date;
- public final class TimeTextView extends TextView {
- public TimeTextView(Context context, AttributeSet attrs) {
- super(context, attrs);
- postDelayed(mUpdateView, mRepeatTimePeriod);
- }
- private void setTimeString() {
- Calendar c = Calendar.getInstance();
- Date currentTime = c.getTime();
- String timeString = formatTime(currentTime);
- setText(timeString);
- }
- private Handler mHandler = new Handler();
- /**
- * one second.
- */
- private int mRepeatTimePeriod = 1000;
- private Runnable mUpdateView = new Runnable() {
- @Override
- public void run() {
- TimeTextView.this.setTimeString();
- // Force toggle again in a second
- mHandler.postDelayed(this, mRepeatTimePeriod);
- }
- };
- private String formatTime(Date time) {
- int hours = time.getHours();
- int miniutes = time.getMinutes();
- int seconds = time.getSeconds();
- String ret = "";
- if (hours < 10) {
- ret += "0";
- }
- ret += hours + ":";
- if (miniutes < 10) {
- ret += "0";
- }
- ret += miniutes + ":";
- if (seconds < 10) {
- ret += "0";
- }
- ret += seconds;
- return ret;
- }
- }
修改main.xml
- <?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"
- />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="0dp"
- android:layout_weight="1.0"
- />
- <com.pstreets.guice.customview.TimeTextView
- android:id="@+id/txtTime"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
定义该TimeTextView的id为txtTime。
这样可以在对应的Activity中使用InjectView 来给对应的txtTime 自动赋值(注入)。
- @ContentView(R.layout.main)
- public class GuiceDemo extends RoboActivity {
- @InjectView (R.id.txtTime) TimeTextView txtTime;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- txtTime.setTextColor(0xFFFF0000);
- }
- }
由于GuiceDemo是从RoboActivity派生的,在onCreate 第一行的super.onCreate() 完成注入功能。如果你对RoboGuice1.1 熟悉的话,在使用注入的变量前如txtTime,需要执行setContentView。
在RoboGuice2.0中你可以使用同样的方法,2.0还提供了一个简单的标注方法@ContentView ,如本例,为Activity设置ContentView。
。
从本例可以看出,和RoboGuice1.1 相比,RoboGuice2.0在使用上要简洁的多。本例只需从RoboActivity派生,不在需要定义Application等。