Android RoboGuice User Guide (18): Inject Resources

Source: Internet
Author: User

Roboguice also provides Injection Support for accessing various resources such as drawable, arrary, and string in the res directory. You can use @ InjectResource to easily apply the required resources.

In this example, the Android ApiDemos sample resolution (48) is modified: Content-> Resources use the Inject Resource method to access Resources.

[Java] public class InjectResourceDemo extends RoboActivity {

@ InjectView (R. id. styled_text) TextView styled_text;
@ InjectView (R. id. plain_text) TextView plain_text;
@ InjectView (R. id. res1) TextView res1;
@ Inject Resources res;
@ InjectResource (R. string. styled_text) String str;

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. injectresource );

// Use res to get the string resources
CharSequence cs = res. getText (R. string. styled_text );
// Note the use
// CharSequence instead of String
// So we don't lose the style info.
Styled_text.setText (cs );

// Use the same resource, but convert it
// A string, which causes it
// To lose the style information.
Plain_text.setText (str );
Res1.setText (cs );

}

}
Public class InjectResourceDemo extends RoboActivity {
 
@ InjectView (R. id. styled_text) TextView styled_text;
@ InjectView (R. id. plain_text) TextView plain_text;
@ InjectView (R. id. res1) TextView res1;
@ Inject Resources res;
@ InjectResource (R. string. styled_text) String str;
 
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. injectresource );
 
// Use res to get the string resources
CharSequence cs = res. getText (R. string. styled_text );
// Note the use
// CharSequence instead of String
// So we don't lose the style info.
Styled_text.setText (cs );
 
// Use the same resource, but convert it
// A string, which causes it
// To lose the style information.
Plain_text.setText (str );
Res1.setText (cs );
 
}
 
}


 

Download this example: http://www.bkjia.com/uploadfile/2012/0507/20120507111059873.zip



Excerpted from the mobile app

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.