内容简介:翻译自:https://stackoverflow.com/questions/7557229/r-string-value-showing-up-as-a-number
对于下面的代码,我打算获取系统日期并根据当前语言环境的格式显示它,它就是R.string.date的日期.在模拟器中,它总是显示为长数字(类似于821302314)而不是“Date:”,我已经在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));
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>
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>
翻译自:https://stackoverflow.com/questions/7557229/r-string-value-showing-up-as-a-number
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
深入理解计算机系统
Randal E.Bryant、David O'Hallaron / 龚奕利、雷迎春 / 中国电力出版社 / 2004-5-1 / 85.00元
从程序员的视角,看计算机系统! 本书适用于那些想要写出更快、更可靠程序的程序员。通过掌握程序是如何映射到系统上,以及程序是如何执行的,读者能够更好的理解程序的行为为什么是这样的,以及效率低下是如何造成的。粗略来看,计算机系统包括处理器和存储器硬件、编译器、操作系统和网络互连环境。而通过程序员的视角,读者可以清晰地明白学习计算机系统的内部工作原理会对他们今后作为计算机科学研究者和工程师的工作有......一起来看看 《深入理解计算机系统》 这本书的介绍吧!