public void DynamicSetTextTool(int stringId, Object changeText, int viewId) {// 動態文本工具方法
String RefreshTime = getResources().getString(stringId);
String FinalRefreshTime = String.format(RefreshTime, changeText);
TextView RefreshTextObject = (TextView) findViewById(viewId);
RefreshTextObject.setText(FinalRefreshTime);
}
使用方法:
DynamicSetTextTool(R.string.cancelfly,
Integer.parseInt(SingleRoadValue[8].toString()),
R.id.cancelfly);
在String.xml裏要定義一個String,寫法如下:
<string name="cancelfly">取消航班:%1$d架次</string>
這個對應的就是R.string.cancelfly
%1$s是字符串類型的佔位符,%1$d是數字類型的佔位符,我的方法裏使用的是Object類型,所以你傳數字或者字符串都可以,自己轉化一下就可以了。
在佈局文件裏要定義一個TextView,寫法如下:
<TextView android:textSize="19px" android:textColor="@color/black" android:id="@+id/cancelfly" android:text="@string/cancelfly"/>
這個對應的是R.id.cancelfly
這樣就等於設置動態的文本了,可以將你從WebService獲取到的值動態的賦值給id爲cancelfly的這個TextView。