Created
April 10, 2015 06:17
-
-
Save shinchit/03fb34fb199db6f562cd to your computer and use it in GitHub Desktop.
Android strings.xmlに可変パラメータを埋め込む
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 「商品名(X)はY円になります」 と出力できるように設定する。X、Yが可変パラメータ --> | |
<string name="sentece">商品名(%1$s)は%2$d円になります</string> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String sentence = getString(R.string.sentence, "ボッコちゃん", 540); | |
// sentenceに構築される文字列は「商品名(ボッコちゃん)は540円になります」になる | |
// 1$, 2$の部分がgetStringに渡される引数(パラメータ)の順番、これを囲む%s, %dは出力フォーマットを表現している | |
// フォーマットに関してはFormatterで規定されている | |
// See Also. http://developer.android.com/reference/java/util/Formatter.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment