實作:
在strings.xml內// <string name="Test_Number">TestNumber %1$d 測試數字</string> <string name="Test_Word">TestNumber %1$s 測試國字</string>
此時要如何在java內操作%1$d和%1$s呢?
先來搞懂上述各代表什麼。
前面的%n代表是第幾個參數
%d(表示整數)
%f(表示浮點數)
%s(表示字符串)
![]() |
| activity_main.xml |
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.chat.a015865.customdialog.MainActivity">
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="onClickMe"
android:text="Hello World!" />
<TextView
android:id="@+id/textViewtwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview"
android:layout_centerHorizontal="true"
android:text="Hello Text Two" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = (TextView) findViewById(R.id.textview);
TextView textviewtwo = (TextView) findViewById(R.id.textViewtwo);
String message = getString(R.string.Test_Number);
String messagetwo = getString(R.string.Test_Word);
textView.setText(String.format(message, 123789));
textviewtwo.setText(String.format(messagetwo, "三十國字哈囉"));
}
}
/**
*String.format(strings,value);
*strings 是strings.xml內的字串參數(ex:TestNumber %1$d 測試數字)
*value 是你帶給%1$d的值
*/
實作完成畫面
![]() |
| 實作完成畫面 |
簡易好懂:
http://blog.csdn.net/xiaoyaovsxin/article/details/8450056
詳細版本:
http://blog.csdn.net/lonely_fireworks/article/details/7962171


沒有留言:
張貼留言