facebook twitter hatena line email

「Android/xml文字列」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(ページの作成:「==stringxmlから置換して文字を設定したい場合== *strings.xml <?xml version="1.0" encoding="utf-8"?> <<nowiki />resources> <<nowiki />string name="descr...」)
 
(stringxmlから置換して文字を設定したい場合)
行8: 行8:
 
*MainActivity.java
 
*MainActivity.java
 
  String description = getString(R.string.description, 4, "aiueo");
 
  String description = getString(R.string.description, 4, "aiueo");
 +
 +
*Activity以外からだと
 +
getApplicationContext().getResources().getString(R.string.description);

2018年7月20日 (金) 13:39時点における版

stringxmlから置換して文字を設定したい場合

  • strings.xml
<?xml version="1.0" encoding="utf-8"?>  
<resources>
<string name="description">test %1$d %2$s</string>
</resources>
  • MainActivity.java
String description = getString(R.string.description, 4, "aiueo");
  • Activity以外からだと
getApplicationContext().getResources().getString(R.string.description);