寫在課前的話:
看帖不回,胸部下垂。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。