Android Roboguice Usage Guide (inject View)

Source: Internet
Author: User

In the example Android Roboguice use Guide (2): The first example Hello World describes the basic steps for using Roboguice development:

Create a roboapplication subclass Guiceapplication,guiceapplication as a appliacation subclass, modify Androidmanifest.xml to Application Name points to this class.

Change the class base class originally derived from the activity to roboactivity (or other related activity).

Overload the Configuatation method definition bindings in Abstractandroidmodule if necessary.

If you do not use Roboguice, if you need to access a view defined in layout in an activity, you generally need to use Findviewbyid to find the corresponding view and cast it to the corresponding class, if you need to access a lot of view, Duplication of code is cumbersome.

If you use Roboguice's inject view, the code is simple and easy to read, and the basic usage of @Inject view is as follows:

@InjectView (r.id.xxx) ViewType viewinstance;

R.id.xxx the ID that is defined in layout for the desired view, such as R.id.textview1

ViewType is the type of view you want, such as TextView

Viewinstance is the variable name.

We define a injectview.xml, as follows:

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

< LinearLayout

Xmlns:android= "Http://schemas.android.com/apk/res/android"

android:orientation= "Vertical"

Android:layout_width= "Match_parent"

android:layout_height= "Match_parent" >

<textview

Android:id= "@+id/textview1″

Android:layout_width= "Fill_parent"

android:layout_height= "Wrap_content"

android:text= "@string/injectview"

/>

<textview

Android:id= "@+id/textview2″

Android:layout_width= "Fill_parent"

android:layout_height= "Wrap_content"

android:text= "@string/injectview"

/>

<textview

Android:id= "@+id/textview3″

Android:layout_width= "Fill_parent"

android:layout_height= "Wrap_content"

android:text= "@string/injectview"

/>

<textview

Android:id= "@+id/textview4″

Android:layout_width= "Fill_parent"

android:layout_height= "Wrap_content"

android:text= "@string/injectview"

/>

<button android:id= "@+id/button"

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

Android:layout_gravity= "Center_vertical"

android:text= "@string/clickmebutton"/>

</LinearLayout>

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.