我看了很多东西,但似乎我的App没有得到strings.xml中字符串的值.它实际上似乎是空白.我认为初始化存在问题.
strings.xml中
<string name="itb_cityID">2</string>
<string name="itb_city">New York</string>
Run Code Online (Sandbox Code Playgroud)
constants.java摘录:
public class ConstantData {
public static String cityID="2";
public static String city="New York";
Run Code Online (Sandbox Code Playgroud)
我该如何设置cityID = R.strings.itb_cityID和city=itb_city正确的方法?
Lef*_*nia 13
String yourString = Context.getResources().getString(R.string.your_string);
注意:您不能Context静态使用.如果您在活动中,只需使用this或getResources()直接致电.否则,您需要获取应用程序上下文的句柄getApplicationContext().
这个例子很有用
保存在res/values/strings.xml的XML文件:
Run Code Online (Sandbox Code Playgroud)<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello!</string> </resources>
此布局XML将字符串应用于View
Run Code Online (Sandbox Code Playgroud)<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)将保留应用于字符串的任何富文本样式.
此示例来自http://developer.android.com/guide/topics/resources/string-resource.html