2017-08-07 177 views
0

有谁知道我的问题的解决方案? 我为游戏中的共享首选项作为活动编辑了一个编辑器。有一个包含一个共享首选项的所有值的列表。但是,当我写了一个名为value_themetextViewAndroid使用错误的字符串资源

value_theme.setText(R.string.editor_div_value + settings_theme);

Android使用来自其他活动的其他字符串资源。

当我写

value_theme.setText(R.string.editor_div_value + "" + settings_theme);

应用程序设置TextView的文本:21312309366.有谁知道如何解决这个问题?

+0

当我用value_theme.setText;(+ settings_theme “当前值”。)一切正常,但我想开发多种语言。 – iamniklas

+0

settings_theme是来自共享偏好的值 – iamniklas

+0

'R.string.editor_div_value'只是一个数字,它就像字符串值的索引。您需要获取字符串值,而不仅仅是引用索引。请参阅@Bob答案示例 – Doomsknight

回答

3

使用此:

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)); 
+0

好吧稍等片刻我试试 – iamniklas

+0

您的意思是getApplicationContext()与上下文吗? – iamniklas

+0

您可以传递任何上下文。活动或应用程序环境。 – Bob