`
isiqi
  • 浏览: 16078349 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

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

 
阅读更多

前面介绍了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等


分享到:
评论

相关推荐

    安卓Android源码——Inject增加sqlite3数据库映射注解(ORM).zip

    安卓Android源码——Inject增加sqlite3数据库映射注解(ORM).zip

    InjectDLL_injectDll_

    Usage: injectdll.exe [process name] [dll path] [option number]option 1 - CreateRemoteThreadoption 2 - NtCreateThreadExoption 3 - RtlCreateUserThread

    protoc-gen-gotag:将自定义结构标签添加到protobuf生成的结构

    原始基因标签(PGGT) PGGT是一个protoc插件,用于在生成的protobuf消息上添加/替换struct标签。...syntax = "proto3" ; package example ; import "tagger/tagger.proto" ; message Example { str

    vue 解决provide和inject响应的问题

    官网上说provide 和 inject 绑定并不是可响应的。这是刻意为之的。然而,如果你传入了一个可监听的对象,那么其对象的属性还是可响应的。 provide: Object | () =&gt; Object(一个对象或返回一个对象的函数) inject...

    Android代码-Codeview

    Codeview is a android library tha lets you preview code in webview very easy and simple with highlighs and colors. With styles to chooses and language. Also you can inject ...

    Android代码-injectio

    Inject.io []() Tiny and nice injections syntax sugar for Android. If you're using heavy injection frameworks just to inject views and resources try this. Wat? Just few lines of code, completely no...

    【JavaScript源代码】vue3中provide和inject的使用.docx

    vue3中provide和inject的使用  1.provide 和 inject 的讲解 provide和inject可以实现嵌套组件之间进行传递数据。 这两个函数都是在setup函数中使用的。 父级组件使用provide向下进行传递数据; 子级组件使用inject...

    custom-plugin:webpack自定义插件

    引子 想要了解webpack plugin如何编写,首先要了解其应用场景和作用。 可以先浏览这三篇文章 ...例如,强大的babel-loader可以使用浏览器暂不支持的JavaScript语法(糖),css-loader和styles-loader用来处

    inject:围绕javax.inject批注的简单Kotlin多平台抽象

    例如,在Kotlin公共代码中的构造函数上使用Inject注释: class MyUseCase @Inject constructor() { .. . } 可用的注释和界面 @Inject @Named("name") @Qualifier @Scope @Singleton Provider 建立图书馆 该库...

    heka-hl-sandboxes:Heka 自定义 lua 沙箱

    DIR=path/to/heka-hl-sandboxes/$ cd $HEKA_HL_DIR$ python -m unittest -b tests或者运行单个测试: $ python -m unittest -b tests.TestLogData配置在[hekad]配置中增加max_message_loops和max_timer_inject全局...

    sisu-inject-bean-1.4.2.jar

    sisu-inject-bean-1.4.2.jar

    mimikatzWIN服务器神器

    用于调试程序 使用方法: 1. 运行主程序:mimikatz.exe ...3. 输入:inject::process lsass.exe sekurlsa.dll 将sekurlsa.dll 注入到lsass.exe 中 4. 输入:@getLogonPasswords 即可获取hash和当前用户的明文密码

    InjectCSSToWebView:Inject CSS To WebView ; 将CSS注入WebView

    InjectCSSToWebView##Inject CSS To WebView ;##将CSS注入WebView .NO.1,Before load WebView,Inject CSS. 在加载WebView之前注入CSS。NO.2,After load WebView,Inject CSS. 在加载WebView之后注入CSS。

    Android注解框架AndroidInject.zip

    AndroidInject 是 Android 注解框架,以简化 Android 开发 目前完成的注解(持续增加中): @AINoTitle: 类注解, 只适用于Activity(需继承于AIActivity), 设置Activity不显示Title  @AIFullScreen: 类注解...

    VC代码 RT_INJECT (实用代码源)

    VC代码 RT_INJECT (实用代码源)VC代码 RT_INJECT (实用代码源)VC代码 RT_INJECT (实用代码源)VC代码 RT_INJECT (实用代码源)VC代码 RT_INJECT (实用代码源)VC代码 RT_INJECT (实用代码源)VC代码 RT_INJECT (实用代码...

    rage_vnc.rar_KAZAA_Morpheus _inject_p2p vnc_rage_vnc

    Functions: Topic parsing, supporting multi-commands Anti-Botkiller Anti-Sandbox VNC-Scanner ...inject disconnect reconnect reconnect.next nick restart vncstop patch part join scan msn

    android_dagger2_sample:匕首2解释

    假设读者在当今流行的android应用程序中需要使用dagger 2。 在此示例中,我想弄清楚Dagger 2的用法,我将尝试以最容易理解的方式描述Dagger的内容,包括图像和代码。 具体而言,该项目需要澄清: 什么是module ? ...

    巫术:注入JS和CSS。「Witchcraft: Inject JS and CSS」-crx插件

    Witchcraft是Google Chrome浏览器的扩展程序,用于直接从文件系统中的文件夹加载自定义Javascript和CSS,并将它们注入与其文件名匹配的页面中。 它通过将每个页面域与scripts文件夹中可用的脚本文件名进行匹配来工作...

    meteor-inject-data:一种使用初始HTML向客户端注入数据的方法

    安装meteor add communitypackages:inject-data推送数据我们需要将此包与服务器端路由器一起使用。 我们扩展了nodejs http.OutgoingMessage并提供了这样的API。 这是的示例。 Picker . route ( '/' , function ( ...

    AndroidKiller小插件,多dex转java源码,无需关闭编译器直接打开即可看

    AndroidKiller小插件,多dex转java源码,无需重启AndroidKiller直接打开jdgui即可看代码,无需手动反编译dex2,3,4,5,省时省力。 ps: 这是解决多dex编译源码的小工具,需要解决编译卡死的,请看我另一个帖子...

Global site tag (gtag.js) - Google Analytics