Android 文字動態處理方法,TextView動態賦值。
阿新 • • 發佈:2019-02-01
publicvoid 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,寫法如下:
在佈局檔案裡要定義一個TextView,寫法如下:
<string name="cancelfly">取消航班:%1$d架次</string>
這個對應的就是R.string.cancelfly
%1$s是字串型別的佔位符,%1$d是數字型別的佔位符,我的方法裡使用的是Object型別,所以你傳數字或者字串都可以,自己轉化一下就可以了。
<TextView android:textSize="19px" android:textColor="@color/black" android:id="@+id/cancelfly" android:text="@string/cancelfly"/>
這個對應的是R.id.cancelfly
這樣就等於設定動態的文字了,可以將你從WebService獲取到的值動態的賦值給id為cancelfly的這個TextView。