Android - Context中的getText(int resId)方法和getString(int resId)方法的区别
Android开发中,经常在Activity中使用getText(int resId)和getString(int resId)这两个方法,那么这两个方法有什么区别和联系呢?
这两个方法的参数都是资源ID,区别在于getText(int resId)返回的是一个CharSequence,而getString(int resId)返回的是一个String。源代码如下:
getText(int resId):
/** * Return a localized, styled CharSequence from the application‘s package‘s * default string table. * * @param resId Resource id for the CharSequence text */ public final CharSequence getText(int resId) { return getResources().getText(resId); }
getString(int resId):
/** * Return a localized string from the application‘s package‘s * default string table. * * @param resId Resource id for the string */ public final String getString(int resId) { return getResources().getString(resId); }
可以看到,他们在各自的内部又调用了Resources类的getText(int id)和getString(int id)方法,那么我们就再看一下Resources类中的这两个方法是怎么写的:
Resources类中的getText(int id):
/** * 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)); }
Resources类中的getString(int 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)); }
看到这里我想大家就都明白了,Resources类的中getString(int id)方法其实就是调用了Resources类的getText(int id)方法后,多做了一个toString()处理,那么也就是说,我们要讨论的问题的结论就可以理解为:Context类中的getString(int resId)==getText(int resId).toString()。
更直观一点,我们来做个小demo:
首先,在Strings中定义两个string资源:
<string name="get_text"><b>getText</b></string> <string name="get_string"><b>getString</b></string>
接着,在layout文件中定义两个TextView,分别是textView1和textView2(代码省略)。
最后,分别使用getText(int resId)和getString(int resId)获取上面两个资源,并在textView1和textView2中显示:
CharSequence charSequence = getText(R.string.get_text); String str = getString(R.string.get_string); ((TextView)findViewById(R.id.textView1)).setText(charSequence); ((TextView)findViewById(R.id.textView2)).setText(str);
结果一目了然:
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。