您的位置:首页 > 移动开发 > Android开发

Android 文本动态处理方法,TextView动态赋值。

2013-05-03 09:03 447 查看
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。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: