這個方法,就是一個佔位符的使用。
很多java代碼都用這樣的用法,android中也有這個用法。
工具/原料
編譯工具:Eclipse
方法/步驟
首先展示代碼結構
頁面類
MainActivity
MainActivity
代碼如下:
public class MainActivity extends ActionBarActivity {
TextView test_fonts; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); test_fonts = (TextView)findViewById(R.id.test_fonts); String test_text = getString(R.string.test_fonts); test_text = String.format(test_text, "我就是測試文字!!!!"); test_fonts.setText(test_text); }
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }
@Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }}
頁面佈局文件
要調用的string.xml
string.xml的類容
string.xml的代碼
最後還是把最關鍵的代碼提出來說一下
項目代碼執行生成的頁面如下:
%[index]$[type]這個是佔位符的編寫格式
例如:用來測試的文字:%1$s!
這樣大家應該看的懂了吧
注意事項
本步驟需要一定代碼基礎
程序代碼以代碼片段的形式展示