facebook twitter hatena line email

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

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(stringxmlから置換して文字を設定したい場合)
行11: 行11:
 
*Activity以外からだと
 
*Activity以外からだと
 
  getApplicationContext().getResources().getString(R.string.description);
 
  getApplicationContext().getResources().getString(R.string.description);
 +
 +
==キー名を変数にしてidを取得する方法==
 +
int strId = getResources().getIdentifier("hogehoge", "string", getPackageName());

2018年12月4日 (火) 16:31時点における版

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);

キー名を変数にしてidを取得する方法

int strId = getResources().getIdentifier("hogehoge", "string", getPackageName());