java 使用带有 Android 的 strings.xml

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5861191/
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-10-30 13:09:23  来源:igfitidea点击:

Using strings.xml w/ Android

javaandroid

提问by basemansix8

I've looked and tried many things but it seems my App isn't getting the values for strings in strings.xml. It actually seems like it's passing blank. I think there's an issue initializing.

我已经查看并尝试了很多东西,但似乎我的应用程序没有获取 strings.xml 中字符串的值。它实际上似乎正在传递空白。我认为初始化存在问题。

strings.xml

字符串.xml

    <string name="itb_cityID">2</string>
        <string name="itb_city">New York</string>

constants.java excerpt:

constants.java 摘录:

public class ConstantData {

public static String cityID="2";
public static String city="New York";

How do I set cityID = R.strings.itb_cityIDand city=itb_citythe correct way?

我如何设置cityID = R.strings.itb_cityIDcity=itb_city正确的方法?

回答by LeffelMania

String yourString = Context.getResources().getString(R.string.your_string);

String yourString = Context.getResources().getString(R.string.your_string);

Note: You can't use Contextstatically. If you're inside an Activity, simply use thisor call getResources()directly. Otherwise you'll need to obtain a handle to your application's context via getApplicationContext().

注意:不能Context静态使用。如果您在 Activity 中,只需直接使用this或调用getResources()即可。否则,您将需要通过 获取应用程序上下文的句柄getApplicationContext()

回答by chihau

This example could be useful

这个例子可能有用

  • XML file saved at res/values/strings.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="hello">Hello!</string>
    </resources>
    
  • This layout XML applies a string to a View

    <TextView
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="@string/hello" />
    
  • This application code retrieves a string:

    String string = getString(R.string.hello);

    You can use either getString(int) or getText(int) to retrieve a string. getText(int) will retain any rich text styling applied to the string.

  • 保存在 res/values/strings.xml 的 XML 文件:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="hello">Hello!</string>
    </resources>
    
  • 此布局 XML 将字符串应用于视图

    <TextView
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="@string/hello" />
    
  • 此应用程序代码检索字符串:

    String string = getString(R.string.hello);

    您可以使用 getString(int) 或 getText(int) 来检索字符串。getText(int) 将保留应用于字符串的任何富文本样式。

This example is from http://developer.android.com/guide/topics/resources/string-resource.html

这个例子来自http://developer.android.com/guide/topics/resources/string-resource.html

回答by steve.watson128

Have you checked that the entire project is error free. If it is not there may be an error that is preventing R.java from being updated with your string. You could try to "Clean" you projected from the "Project" menu. That may work. Also, I can't see why you would have to use Context., usually you should be able to call getResources().directly (in some situations even this is unnecessary). A good thing to look for is if the string you have declared in your strings.xml file is recognised as a declared string.

您是否检查过整个项目没有错误。如果不是,则可能存在阻止 R.java 使用您的字符串更新的错误。您可以尝试从“项目”菜单中“清理”您投影的内容。那可能会奏效。另外,我不明白为什么您必须使用Context.,通常您应该能够getResources().直接调用(在某些情况下甚至这是不必要的)。一个好消息是,您在 strings.xml 文件中声明的字符串是否被识别为声明的字符串。