的setText(的getString(R.strings.whatever)或的setText(R.strings.whatever)? [英] setText(getString(R.strings.whatever) or setText(R.strings.whatever)?

查看:99
本文介绍了的setText(的getString(R.strings.whatever)或的setText(R.strings.whatever)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两个工作,很明显,如果你开始串联,你需要得到的字符串,以避免显示一个int。

Both work, obviously if you start concatenating you'll need get string in order to avoid displaying an int.

问:这是最优雅或推荐使用

Question: which is the most 'elegant' or 'recommended' to use?

感谢您

推荐答案

第二种方法是更优雅,因为内部的的TextView (或任何查看级)会做的越来越字符串为您指定的资源的工作。

The second approach is more elegant, since internally, the TextView (or whatever View-class) will do the job of getting the String for your specified resource.

放生组件做内部工作总是preferred。此外,它是更短,更具有可读性。

Letting components do the internal work is always preferred. Also, it is shorter and more readable.

关于我谈到了内部:如果您对机器人源 - code一看,你可以看到的setText(INT) -method的是,的TextView <一个href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/2.2_r1.1/android/widget/TextView.java#TextView.setText%28int%29"相对=nofollow>这样的实现:

About the internals I talked about: If you have a look on Androids source-code, you can see that the setText(int)-method of TextView is implemented like this:

public final void setText(int resid) {
  setText(getContext().getResources().getText(resid));
}

所以,它在内部使用了上下文级获得的资源ID字符串。现在,如果你看看的getText() - 方法(这也来自于上下文级),你可以看到,它<一个href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/2.1_r2/android/content/Context.java#Context.getString%28int%29"相对=nofollow>实施方式相同:在

So, it internally uses the Context-class to get the string from the resource-id. Now, if you look at the getText()-method (which also comes from the Context-class), you can see that it is implemented the same way:

public final String getString(int resId) {
  return getResources().getString(resId);
}

因此​​,对于性能和可靠性的原因,这都没有区别。尽管如此,它短小,可读性更强:D

So for performance or reliability reasons, it makes no difference. Still, it is shorter and more readable :D

这篇关于的setText(的getString(R.strings.whatever)或的setText(R.strings.whatever)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆