Android RoboGuice2 Usage Guide (3) Inject custom view

Source: Internet
Author: User
Tags current time

The HelloWorld example of the Android RoboGuice2 is described earlier, and the main considerations for upgrading from Roboguice 1.1 to RoboGuice2.0 are described.

This example describes how to inject custom View,inject view and Android's own view (such as Textview,button) method.

This example uses a custom TextView to display the current time every 1 seconds. The preceding definitions are as follows:

---------------------------------PACKAGE------------------------------------PACKAGE com.pstreets.guice.custom          
View     
---------------------------------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, attributeset a     
        Ttrs) {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 <) {ret = "0";     
        RET + + hours + ":";     
        if (Miniutes <) {ret = "0";     
        RET + + miniutes + ":";     
        if (seconds <) {ret = "0";   ret = seconds;  
          
        return ret; }     
          
}

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

The ID of the Timetextview is defined as txttime.

This allows the Injectview to be used in the corresponding activity to automatically assign (inject) the corresponding 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);     
          
    }     
          

Since Guicedemo is derived from roboactivity, the Super.oncreate () completes the injection function on the first line of OnCreate. If you are familiar with RoboGuice1.1, you need to perform setcontentview before using the injected variables such as txttime.

In RoboGuice2.0 you can use the same method, 2.0 also provides a simple notation method @contentview, as in this example, set Contentview for the activity.

This example downloads: Http://www.imobilebbs.com/download/android/RoboGuiceDemo1.zip

We can see from this example, compared with RoboGuice1.1, RoboGuice2.0 in the use of more concise. This example simply derives from roboactivity and does not need to define application, etc.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.