两者都有效,显然,如果您开始连接,则需要获取字符串以避免显示 int。
问题:哪个是最“优雅”或“推荐”使用的?
谢谢
最佳答案
第二种方法更优雅,因为在内部,TextView
(或任何 View
类)将完成为指定资源获取字符串的工作。
让组件做内部工作总是首选。此外,它更短且更易读。
关于我谈到的内部结构:如果你看一下 Android 源代码,你可以看到 setText(int)
-TextView
is implemented like this 的方法:
public final void setText(int resid) {
setText(getContext().getResources().getText(resid));
}
因此,它在内部使用 Context
类从资源 ID 中获取字符串。现在,如果您查看 getText()
方法(它也来自 Context
类),您可以看到它是 is implemented the same way :
public final String getString(int resId) {
return getResources().getString(resId);
}
因此出于性能或可靠性原因,这没有区别。不过,它更短且更易读。
关于android - setText(getString(R.strings.whatever) 或 setText(R.strings.whatever)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10120867/