Android 文本動態處理方法,TextView動態賦值。


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。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章