您的位置:首页 > 其它

getText 与getString 区别

2015-04-16 15:51 281 查看
Android提供多种获取资源文件方法,但是需要注意以下方法:

CharSequence getText(int resId):返回本地、样式化的字符。
String getString(int resId) :返回字符串

/**
* Return the string value associated with a particular resource ID.  The
* returned object will be a String if this is a plain string; it will be
* some other type of CharSequence if it is styled.
* {@more}
*
* @param id The desired resource identifier, as generated by the aapt
*           tool. This integer encodes the package, type, and resource
*           entry. The value 0 is an invalid identifier.
*
* @throws NotFoundException Throws NotFoundException if the given ID does not exist.
*
* @return CharSequence The string data associated with the resource, plus
*         possibly styled text information.
*/
public CharSequence getText(int id) throws NotFoundException {
CharSequence res = mAssets.getResourceText(id);
if (res != null) {
return res;
}
throw new NotFoundException("String resource ID #0x"
+ Integer.toHexString(id));
}


/**
* Return the string value associated with a particular resource ID.  It
* will be stripped of any styled text information.
* {@more}
*
* @param id The desired resource identifier, as generated by the aapt
*           tool. This integer encodes the package, type, and resource
*           entry. The value 0 is an invalid identifier.
*
* @throws NotFoundException Throws NotFoundException if the given ID does not exist.
*
* @return String The string data associated with the resource,
* stripped of styled text information.
*/
public String getString(int id) throws NotFoundException {
CharSequence res = getText(id);
if (res != null) {
return res.toString();
}
throw new NotFoundException("String resource ID #0x"
+ Integer.toHexString(id));
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐