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