getString (int ResID)和getText (int ResID)都是Resources类中方法,都是获取资源文件中的字符串资料。
下面先看看二者在API的定义:
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.
返回与特定资源ID相关联的字符串值。如果是无格式的字符串,则返回的是字符串对象,如是格式的字符串,则将返回CharSequence 其他类型。
ResID: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.
CharSequence :The string data associated with the resource, plus possibly styled text information.(与资源想关联的字符串数据和可能有的文本信息样式)
Return the string value associated with a particular resource ID. It will be stripped of(剥夺) any styled text information.
返回与特定资源ID相关联的字符串值。返回的字符串值被去除了全部文本信息的样式
ResID :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.
String :The string data associated with the resource, stripped of styled text information.
二者都是在Resource类中的定义的方法,都是获取资源文件中的字符串资料。
例如:
Strings.xml文件内容如下:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="demo"> <b>demo</b> </string> </resources>
在主程序中的主要语句:
CharSequence chs = getText(R.string.demo); //包含文本的样式信息 String str = getString(R.string.demo); //没有任何的文本样式信息 Text1.setText(chs); Text2.setText(str);