XML字符串资源VS Java的常量字符串? [英] XML string resources vs Java Constant Strings?

查看:344
本文介绍了XML字符串资源VS Java的常量字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串就像字符串数据=应用程序;

我知道加载的字符串在Android中有两种方式。

第一个是

所以它是一个恒定的我把它定义为

 公共静态最终字符串数据=应用程序;
 

和其他类型的defing它 RES / vslues / strings.xml中文件如..

 <字符串名称=数据>应用程序< /串>
<字符串名称=参考hello world>世界,你好<!/串>
 

如果我想使用它..

对于第一种方式 ClassName.data

对于第二种方式 context.getResources()的getString(RESOURCEID)

  

问:

     

所以现在我的问题是我想用相同的字符串在不同classes.And 30次,我有更多的一些variables.so的,这将加载速度更快,并采取较小存储器中的上述方法..

解决方案

不过,速度应该不是真的在这两种情况下的问题。我建议组织根据情理之中的事情。

常量类

把,将在内部使用,如数据库列名或其他键的字符串常量。

的strings.xml

将被显示给用户的字符串。这样,您就可以利用本地化等。

  

根据要求,你应该preFER第二种方法意味着XML   为主。

I have a String is like String data="apps";

i know loading String in Android in two ways..

First one is

so it is a constant i defined it as

public static final String data="apps";

And another type is defing it in res/vslues/strings.xml file like..

<string name="data">apps</string>
<string name="hello_world">Hello world!</string>

if i want to use it..

for the first way ClassName.data

for second way context.getResources().getString(resourceid)

Question:

so now my question is I want to use same String in 30 times in different classes.And I have more number of variables.so which will load faster and take lesser memory in the above methods..

解决方案

However, speed shouldn't really be an issue in either case. I would recommend organizing based on what makes sense.

Constants class

Put strings constants that will be used internally, like database column names or other keys.

strings.xml

Put strings that are displayed for the user. This way you can take advantage of localization, etc.

As per requirement you should be prefer second approach means XML based.

这篇关于XML字符串资源VS Java的常量字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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