java hardcode_hardcode string vs @string in Java code - Android

可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):

问题:

I'm just wondering what the benefits/overheads are for using @string rather than hard coding strings within the actual Java code... For Example: // To get the string resource: getActivity.setTitle(getString(R.string.my_string));

Is this the best practice for Things like Actionbar titles, Dynamically created button text, ect... Or should I just do this: // Hardcoded string getActivity.setTitle("My String");

I know there will be a bit more overhead doing it the first way.. Just not sure what best practice is.

回答1:

Incase you were unaware as to the actual point of having the @string system please read over the localization documentation. It allows you to easily locate text in your app and later have it translated.

Edit Thanks to Hippo for clearing this up.

Using multiple strings of the same value no matter the method (Strings.xml vs programatically) doesn't seem to have any associated overhead. According to Oracle "All literal strings and string-valued constant expressions are interned" which means that the object is reused rather than re-created if you use it again.

回答2:

It is not good practice to hard code strings into your layout files/ code. You should add them to a string resource file and then reference them from your layout.

Reasons: This allows you to update every occurrence of the same word in all layouts at the same time by just editing your strings.xml file.

It is also extremely useful for supporting multiple languages as a separate strings.xml file can be used for each supported language.Suppose that your application's default language is English. Suppose also that you want to localize all the text in your application to French, and most of the text in your application (everything except the application's title) to Japanese. In this case, you could create three alternative strings.xml files, each stored in a locale-specific resource directory

I think these reasons are enough to recomend to go for @String

回答3:

That way you have a fixed place to alter all your strings within the project. Lets say you used same string in 10 different locations in the code. What if you decide to alter it? Instead of searching for where all it has been used in the project you just change it once and changes are reflected everywhere in the project.

回答4:

Well strings.xml would have to be parsed, wouldn't it? Then I suppose hard coded would be best for performance, though probably unnoticeable at runtime. People do choose to use it though in order to have all the strings in one spot in case there are plans to translate the app.

回答5:

There are many benefits to setting the strings in a strings.xml file; in a nutshell, it allows you to use the same string in multiple locations, which is good if you somehow need to modify the string later. It also allows you to display the same text in different languages; hardcoding the string doesn't give you all those options.

BTW, you don't need to put every text in the strings.XML file; only the ones that might be used in multiple places in the application.

The general rule for placing strings in the strings.xml file are these: Will it be used in multiple locations?

Will it be used in multiple languages?

Will it be dynamic or static?

I'm sure there are other reasons, but these are the ones I know.

Hopefully, this helps.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值