标签:android style class blog code java
一是可以通过Java的 String.format(String format, Object... args)
方法来实现
二则是通过Android自带的 getResources().getString(int id, Object... formatArgs)
实现
占位符的语法可以参考Java文档
简单演示下第二种方法
strings.xml
1 <string name="boolean_conversion">Boolean: %1$b\n</string> 2 <string name="string_conversion">String: %1$s\n</string> 3 <string name="integer_conversion">Integer: %1$d\n</string> 4 <string name="float_conversion">Float: %1$.2f\n</string> 5 <string name="date_or_time_conversion">Year: %1$tY, Month: %1$tM, Day:%1$td</string>
MainActivity.java
1 public class MainActivity extends ActionBarActivity { 2 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.ac_main); 7 8 TextView text = (TextView) findViewById(R.id.ac_main_text); 9 StringBuilder builder = new StringBuilder(); 10 builder.append( 11 getResources().getString(R.string.boolean_conversion, true)); 12 13 builder.append( 14 getResources().getString(R.string.string_conversion, "hello world")); 15 16 builder.append( 17 getResources().getString(R.string.integer_conversion, 1234)); 18 19 builder.append( 20 getResources().getString(R.string.float_conversion, 1234.5678)); 21 22 Calendar calendar = Calendar.getInstance(); 23 builder.append( 24 getResources().getString(R.string.date_or_time_conversion, calendar)); 25 26 text.setText(builder.toString()); 27 } 28 }
Android字符串中使用占位符,布布扣,bubuko.com
标签:android style class blog code java
原文地址:http://www.cnblogs.com/xiaogengfanlucao/p/3795301.html