Android使用错误的字符串资源
问题描述:
有谁知道我的问题的解决方案? 我为游戏中的共享首选项作为活动编辑了一个编辑器。有一个包含一个共享首选项的所有值的列表。但是,当我写了一个名为value_theme
textView
:Android使用错误的字符串资源
value_theme.setText(R.string.editor_div_value + settings_theme);
Android使用来自其他活动的其他字符串资源。
当我写
value_theme.setText(R.string.editor_div_value + "" + settings_theme);
应用程序设置TextView的文本:21312309366.有谁知道如何解决这个问题?
答
使用此:
value_theme.setText(context.getString(R.string.editor_div_value) + "" + settings_theme);
因为您将字符串,你错误地使用了错误setText
方法,它接受一个CharSequence
并设置到TextView
,因为它是。
或者另一种方式是做字符串格式化。
<string name="editor_div_value">Your String value %1$s</string>
要获得在Java代码中的字符串:
value_theme.setText(context.getString(R.string.editor_div_value, settings_theme));
当我用value_theme.setText;(+ settings_theme “当前值”。)一切正常,但我想开发多种语言。 – iamniklas
settings_theme是来自共享偏好的值 – iamniklas
'R.string.editor_div_value'只是一个数字,它就像字符串值的索引。您需要获取字符串值,而不仅仅是引用索引。请参阅@Bob答案示例 – Doomsknight