如何删除错误:-hardcoded string button should use @string resource in eclipse?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14940255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 20:01:29  来源:igfitidea点击:

How to remove error:-hardcoded string button should use @string resource in eclipse?

androideclipse

提问by Janu Bajaj

The error appears when we create a second main.java file to connect it with intial main_activity file?

当我们创建第二个 main.java 文件以将其与初始 main_activity 文件连接时出现错误?

回答by user1888162

Add this to xml: tools:ignore="HardcodedText"

将此添加到 xml:tools:ignore="HardcodedText"

Example:

例子:

  <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text" 
        tools:ignore="HardcodedText"/>

回答by ebarrenechea

In Eclipse you can just open your layout file and right click on the button (or any other view with text) and select Edit Text. From there just select one of the values from the list. If the value you want is not on the list then click on New String...to create a new android string. All your strings are defined in strings.xmlinside your res/values folder.

在 Eclipse 中,您只需打开布局文件并右键单击按钮(或任何其他带有文本的视图)并选择Edit Text. 从那里只需从列表中选择一个值。如果您想要的值不在列表中,则单击New String...以创建新的 android 字符串。所有字符串都在strings.xmlres/values 文件夹中定义。

回答by Boardy

The string for the button should be in your strings.xml inside res/values. This then then means that you can have a strings.xml file for each language your app will support and the correct language will be used based on what locale their device is set to.

按钮的字符串应该在 res/values 中的 strings.xml 中。这意味着您可以为您的应用程序将支持的每种语言创建一个 strings.xml 文件,并且将根据其设备设置的区域设置使用正确的语言。

A simple way to create the string is if you highlight your hard coded string and then go to the menu > refactor > extract android string (It's something similar, I can't remember exactly at the top of my head) and then it will ask you what to the call new string and place it in the string.xml and replace your hard coded string with @string/your_string_name.

创建字符串的一种简单方法是,如果您突出显示您的硬编码字符串,然后转到菜单 > 重构 > 提取 android 字符串(类似的东西,我记不清了在我的头顶),然后它会问您要调用什么新字符串并将其放在 string.xml 中,并用@string/your_string_name 替换您的硬编码字符串。

回答by Sidharath

If you want to set the name of your button. You have to use the strings.xml file in values folder to set the name and use it on Button using @string/(name specified in string.xml file) to solve the problem.

如果要设置按钮的名称。您必须使用 values 文件夹中的 strings.xml 文件来设置名称,并使用 @string/(string.xml 文件中指定的名称) 在 Button 上使用它来解决问题。