TextView的setText注意事项
2015-12-10 10:56
387 查看
##Do not concatenate text displayed with setText
当使用下面的方法对TextView进行setText时
会有如下提示
Do not concatenate text displayed with setText. Use resource string with placeholders.
When calling TextView#setText:
Never call Number#toString() to format numbers; it will not handle fraction separators and locale-specific digits properly. Consider using String#format with proper format specifications (%d or %f) instead.
Do not pass a string literal (e.g. "Hello") to display text. Hardcoded text can not be properly translated to other languages. Consider using Android resource strings instead.
Do not build messages by concatenating text chunks. Such messages can not be properly translated.
简单来说使用TextView的setText方法时,有三点要注意的:
如果有数字,使用String#format方法来格式化
别硬编码,而是使用Android的String资源文件
别使用+进行字符串拼接
##例子
在String资源文件中声名
在TextView中通过setText设置
##参考
stackoverflow中的问题
Android官方文档
当使用下面的方法对TextView进行setText时
prodNameView.setText("" + name); prodOriginalPriceView.setText("" + String.format(getString(R.string.string_product_rate_with_ruppe_sign), "" + new BigDecimal(price).setScale(2, RoundingMode.UP)));
会有如下提示
Do not concatenate text displayed with setText. Use resource string with placeholders.
When calling TextView#setText:
Never call Number#toString() to format numbers; it will not handle fraction separators and locale-specific digits properly. Consider using String#format with proper format specifications (%d or %f) instead.
Do not pass a string literal (e.g. "Hello") to display text. Hardcoded text can not be properly translated to other languages. Consider using Android resource strings instead.
Do not build messages by concatenating text chunks. Such messages can not be properly translated.
简单来说使用TextView的setText方法时,有三点要注意的:
如果有数字,使用String#format方法来格式化
别硬编码,而是使用Android的String资源文件
别使用+进行字符串拼接
##例子
在String资源文件中声名
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
在TextView中通过setText设置
hello.setText(getString(R.string.welcome_messages,"John",10));
##参考
stackoverflow中的问题
Android官方文档
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories