对于下面的代码,我打算获取系统日期并根据当前语言环境的格式显示它,它只是用于 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>
最佳答案
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>
关于android - R.string.value 显示为数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7557229/