R.string.value 显示为数字

发布于 2024-12-07 03:49:20 字数 756 浏览 2 评论 0原文

对于下面的代码,我打算获取系统日期并根据当前语言环境的格式显示它,这只是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小忆控 2024-12-14 03:49:21

为了将所有 "R.string.*" 覆盖为 "getString(R.string.)"* 我写了一些正则表达式。

此正则表达式还会忽略前面已有“getString”的字符串。

((?!getString\() R\.string\.[a-zA-Z1-9_]+)

您只需在 Android Studio 中按 Strg+Shift+R 打开替换终端,然后将上面的正则表达式插入为“查找”,将下面的正则表达式插入为“替换”。

 getString\( $1 \)

不要忘记设置“正则表达式”复选框。

对我来说这非常有效。但是“查找正则表达式”有一个问题,它只能找到以空格开头的 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.

((?!getString\() R\.string\.[a-zA-Z1-9_]+)

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.

 getString\( $1 \)

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.

末蓝 2024-12-14 03:49:20

R.string.date 确实是一个 int,您错过了对 getText()getString()

mTimeText.setText(getText(R.string.date) + " " + dateFormat.format(date));

更好的是,不要在代码中构建字符串,但使用带有 getString( int resId, Object... formatArgs)

mTimeText.setText(getString(R.string.date, dateFormat.format(date)));

并在您的 string.xml 中:

<string name="date">Date: %s</string>

R.string.date is indeed an int, you're missing the call to getText() or getString():

mTimeText.setText(getText(R.string.date) + " " + dateFormat.format(date));

Even better, don't build the string in your code, but use a template with getString(int resId, Object... formatArgs):

mTimeText.setText(getString(R.string.date, dateFormat.format(date)));

and in your string.xml:

<string name="date">Date: %s</string>
绾颜 2024-12-14 03:49:20

是的,如果您使用 R.string.date,您将获得字符串的 ID。
文档中所述

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

示例:

 this.getString(R.string.date);

在此处阅读:getString

Yes, you will get the ID of the String if you use R.string.date.
As stated in the docs

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.

Example:

 this.getString(R.string.date);

Read about it here: getString

拥抱我好吗 2024-12-14 03:49:20

要从 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 be mTimeText.setText(this.getString(R.string.date) + " " + dateFormat.format(date));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文