硬编码字符串在Eclipse [英] Hardcoding Strings in Eclipse

查看:480
本文介绍了硬编码字符串在Eclipse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然在一个应用程序的工作,我注意到,每当我不使用@string /\"...

While working on an app, I noticed that whenever I don't use "@string/"...

的android:文本=@字符串/字符串名称

android:text="@string/stringName"

和只写

的android:文本=字符串名称

android:text="stringName"

Eclipse的希望就是我重新添加@字符串/。这是为什么?这有什么错硬编码字符串?

Eclipse want's me to add back "@string/". Why is that? What's wrong with hardcoding the string?

推荐答案

如果您使用字符串资源,您可以轻松将您的应用程序。例如你可以把你的字符串资源投入价值-DE文件夹和翻译他们把它翻译成德语。那么Android将挑选基于手机的当前语言环境的权利字符串。这也增加了该应用的效率。

You can easily translate your app if you use string resources. For example you can translate it into german, just by putting your string resources into the values-de folder and translating them. Android will then pick the right strings based on the current locale of the phone. It also increases the efficiency of the app.

这是什么它可能看起来像:

This is what it can look like:

/values​​/strings.xml:结果

/values/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Simple Calculator</string>
    <string name="welcome_message">Welcome!</string>

</resources>

/values​​-de/strings.xml(德国):结果

/values-de/strings.xml (german):

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Simpler Taschenrechner</string>
    <string name="welcome_message">Willkommen!</string>

</resources>

编辑:

就像@EGHDK提及;如果你想改变一些文字可以节省您的大量时间。使用字符串资源你有一个地方的一切。

Just like @EGHDK mentioned; it saves you a lot of time, if you want to change some text. Using string resources you have everything in one place.

这篇关于硬编码字符串在Eclipse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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