当前位置:操作系统 > 安卓/Android >>

Android RoboGuice2 使用指南(3): Inject 自定义View

本例使用一个自定义的TextView,每隔1秒显示当前时间。前定义如下:

[java]
//--------------------------------- 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; 
    } 
  

//--------------------------------- 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

[html] 
<?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:lay

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,