Android RoboGuice使用指南(18):Inject Resources

來源:互聯網
上載者:User

Roboguice 對訪問res 目錄下各種資源drawable, arrary, string 等也提供了注入支援。可以通過@InjectResource 很方便的應用所需資源。

本例修改Android ApiDemos樣本解析(48):Content->Resources->Resources 使用Inject Resource方法來訪問資源。

[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 of  
 // CharSequence instead of String  
 // so we don't lose the style info.  
 styled_text.setText(cs); 
  
 // Use the same resource, but convert it to  
 // 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 of
 // CharSequence instead of String
 // so we don't lose the style info.
 styled_text.setText(cs);
 
 // Use the same resource, but convert it to
 // a string, which causes it
 // to lose the style information.
 plain_text.setText(str);
 res1.setText(cs);
 
 }
 
}


 

本例下載:http://www.bkjia.com/uploadfile/2012/0507/20120507111059873.zip



摘自 引路蜂移動軟體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.