如何在Eclipse中添加字符串资源?
我可以让 Eclipse 在编码时添加字符串资源吗?还是必须始终切换到 string.xml 并添加每个字符串?
Can I have Eclipse adding my string resources as I code or do I have to switch to string.xml all the time and add each string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Eclipse 会帮你做这件事。因此,如果您有一个字段:
选择“hello”,然后转到 Refactor-->Android-->Extract Android String,Eclipse 会将该行更改为:
并自动将该行添加到 strings.xml 中,如下所示:
JAL
Eclipse will sort of do it for you. So if you have a field:
Select "hello" and then goto Refactor-->Android-->Extract Android String, Eclipse will change the line to:
and automagically add the line to strings.xml as:
JAL
Eclipse 为此提供了很棒的节省时间的快捷方式!
1.- 在 XML 编辑器中:
假设您有一个 Button、TextView 或任何带有硬编码字符串作为文本的视图:
将光标放在字符串文字上,按 CTRL+1,然后选择“提取 Android 字符串”。
选择所需的资源名称(例如 my_string_resource),然后单击“确定”。您将在 strings.xml 文件中获得一个字符串资源:
您的按钮现在将如下所示:
全部自动进行,无需进行任何上下文更改:)
2.- 在代码编辑器中:
编写一个字符串 然后选择字符串文字
(从“到”),然后按 CTRL+1,将出现一个黄色菜单,双击“提取 Android 字符串”(在这种情况下,S 键对我不起作用,我只需双击该选项)。选择所需的名称(例如 my_string_resouce),然后单击“确定”。同样,您将获得一个新的 strings.xml 条目:
并且您的 Button 的 setText 行替换为:
希望它能帮助您并为您节省与我一样多的时间! :)
Eclipse has wonderful time-saving shortcuts for this!
1.- in XML editor:
Say you have a Button,TextView, or any View with a hard-coded string as text:
With the cursor over the string literal, press CTRL+1, then choose "Extract Android String".
Choose the desired resource name (e.g. my_string_resource) then click OK. You will get a string resource in your strings.xml file:
And your Button is now gonna look like:
All automatically and without a single context-change :)
2.- In Code editor:
Write a string literal in your code like
Then select the string literal (from " to "), and press CTRL+1, a yellow menu will appear, double click on "Extract Android String" (the S key does not work for me in this case, i just double click on the option). Choose the desired name (e.g. my_string_resouce), and click Ok. Again, you will get a new strings.xml entry:
And your Button's setText line replaced by:
Hope it helps and saves you as much time as it did for me! :)
最佳实践是在
values
文件夹中也有strings.xml
来保存所有字符串常量。因为稍后如果您想进行任何更改,如果您保留在 strings.xml 中,那么对您来说会很容易。否则,您将必须始终记住写入该常量的文件。The best practice is too have
strings.xml
insidevalues
folder which keeps all string constants. Because later on if you want to make any change, it will easy for u if u keep in strings.xml. Otherwise you will have to always remember the file where u have wrote that constant.您必须切换到
string.xml
:这很不幸,但现在 Eclipse 并没有为您提供直接从您正在键入的代码弹出到字符串编辑器的干净方法。最好你输入一个字符串常量(例如R.string.new_string
),我猜热键或双击或其他东西,然后使用现有条目直接跳转到strings.xml编辑器 存在)或创建一个新条目(如果new_string
尚不存在)。选择(如果
new_string
You have to switch to
string.xml
: its unfortunate, but right now Eclipse doesn't give you a clean way of popping into the string editor directly from the code you are typing. Optimally you would type a string constant (likeR.string.new_string
and I guess hotkey or double click or something and jump directly into the strings.xml editor with the existing entry selected (ifnew_string
exists) or a new entry created (ifnew_string
doesn't yet exist).Wouldn't that be nice.