R.string.value 显示为数字
对于下面的代码,我打算获取系统日期并根据当前语言环境的格式显示它,这只是 R.string.date
的代码。在模拟器中,它总是显示为一个长数字(类似于 821302314),而不是我已经在 string.xml
中外部化的“日期:”。谁能帮忙看看为什么会这样?
final TextView mTimeText = (TextView)findViewById(R.id.mTimeText);
//get system date
Date date = new Date();
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
mTimeText.setText(R.string.date + " " + dateFormat.format(date));
布局.xml
<TextView
android:id="@+id/mTimeText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/date"
/>
字符串.xml
<string name="date">Date:</string>
For the below code I intended to get the system date and display it as per the formatting of the current locale, it's just that for the R.string.date
. In emulator it always shows up as a long number (something like 821302314) instead of "Date: " which I has already externalized in the string.xml
. Can anyone help to have a look why this is so?
final TextView mTimeText = (TextView)findViewById(R.id.mTimeText);
//get system date
Date date = new Date();
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
mTimeText.setText(R.string.date + " " + dateFormat.format(date));
layout.xml
<TextView
android:id="@+id/mTimeText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/date"
/>
strings.xml
<string name="date">Date:</string>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了将所有 "R.string.*" 覆盖为 "getString(R.string.)"* 我写了一些正则表达式。
此正则表达式还会忽略前面已有“getString”的字符串。
您只需在 Android Studio 中按 Strg+Shift+R 打开替换终端,然后将上面的正则表达式插入为“查找”,将下面的正则表达式插入为“替换”。
不要忘记设置“正则表达式”复选框。
对我来说这非常有效。但是“查找正则表达式”有一个问题,它只能找到以空格开头的 R.string。我不知道如何解决这个问题,因为如果我删除空格,我也会找到已经有“getString”的 R.string。
也许有人可以帮助改进正则表达式或有更好的方法来实现这一点。
To override all "R.string.*" to "getString(R.string.)"* i wrote a little regex.
This regex also ignores the strings who already have a "getString" in front.
You just have to press Strg+Shift+R in Android Studio to open the replace terminal and insert the regex above as "Find" and as "Replacement" the regex below.
Don't forget to set "regular expression" checkbox.
For me this worked perfectly. But the "Find Regex" got one problem it only finds R.string when it starts with a whitespace. I don't know how to solve this because if i delete the whitespace ill find also the R.string that allready have the "getString".
May some can help to improve the regex or has a better way to achieve this.
R.string.date
确实是一个int
,您错过了对getText()
或getString()
:更好的是,不要在代码中构建字符串,但使用带有
getString( int resId, Object... formatArgs)
:并在您的
string.xml
中:R.string.date
is indeed anint
, you're missing the call togetText()
orgetString()
:Even better, don't build the string in your code, but use a template with
getString(int resId, Object... formatArgs)
:and in your
string.xml
:是的,如果您使用 R.string.date,您将获得字符串的 ID。
如文档中所述
示例:
在此处阅读:getString
Yes, you will get the ID of the String if you use R.string.date.
As stated in the docs
Example:
Read about it here: getString
要从 xml 获取字符串值,您应该调用
this.getString(R.id.nameOfString)
。在您的情况下,这将是mTimeText.setText(this.getString(R.string.date) + " " + dateFormat.format(date));
To get string value from xml, you should call
this.getString(R.id.nameOfString)
. In your case this would bemTimeText.setText(this.getString(R.string.date) + " " + dateFormat.format(date));