2012年8月14日火曜日

AndroidAnnotations -Resources-

今回はリソースの扱い方について。
androidではstring.xml等の設定ファイルに定数を定義して
それを各モジュールで使用するのんだけど、それの取得方法が意外と面倒くさい。
通常の取得方法はこんな感じになります。
  1. public class SampleActivity extends Activity {  
  2.   
  3.     @Override  
  4.     public void onCreate(final Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.   
  7.         // string.xmlから"hoge"で定義されている文字列を取得する  
  8.         final String hoge = getString(R.string.hoge);  
  9.     }  
  10. }  
これを、AndroidAnnotationsで書くと
  1. @EActivity  
  2. public class SampleActivity extends Activity {  
  3.   
  4.     // 引数を指定しない場合は、フィールド名から取得する  
  5.     @StringRes  
  6.     protected String hoge;  
  7.   
  8.     // 引数を指定した場合は指定したものを取得する  
  9.     @StringRes(R.string.hello)  
  10.     protected String fuga;  
  11.   
  12. }  
上記の他にも、@ColorResアノテーションでcolorのint値だったりが取得できます。
設定ファイルで定義しておけるものはほとんど定義できますので詳しくは、 https://github.com/excilys/androidannotations/wiki/Resourcesを参照してください。

1 件のコメント:

  1. If you need to hire a real hacker to remotely monitor / hack your partner's phone, recover your stolen bitcoin / any other cryptocurrency, or hack a database with guaranteed privacy, contact easybinarysolutions@gmail.com or whatsapp: +1 3478577580, they are efficient and confidential.

    返信削除