2017-05-04 79 views
0

你好,我正在关注学习Android的书籍教程,但这本书已经过时了。我得到一个“不能翻译的字符串文字”和“不要连接文本与setText”。我想知道什么是更新TextView的正确方法?我认为这就是我基本上想要做的。如何正确更新textView?

<TextView 
    android:id="@+id/textScore" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Score: 999" 
    android:textSize="36sp" 
    android:layout_alignParentBottom="true" 
    android:layout_toLeftOf="@+id/textOperator" 
    android:layout_toStartOf="@+id/textOperator" /> 

<TextView 
    android:id="@+id/textLevel"`enter code here` 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Level: 4" 
    android:textSize="36sp" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" 
    android:layout_marginRight="11dp" 
    android:layout_marginEnd="11dp" /> 

这里是Java。

textObjectScore.setText("Score:" + currentScore); 
    textObjectLevel.setText("Level:" + currentLevel); 
+0

如果你创建了一个'String textToSet =“Score:”+ currentScore;'then textObjectScore.setText(textToSet);'? –

回答

2

这两个都与翻译有关。

“字符串文字不能被翻译”。这是一条警告,告诉您无论用户将手机改为何种语言,该字符串都将以英文显示。对于专业应用程序,您可以在strings.xml中定义您的字符串,并在您的应用程序中使用该字符串。这允许为手机的语言环境选择字符串,假设您提供了翻译文件。

“不要连接文本和setText”。以您的母语进行此操作并没有实际的问题。问题在于,在其他语言中,你在做什么可能不会产生语法意义。相反,您应该在strings.xml中使用输入变量来定义一个字符串,并使用getString来填充这些变量。

但是为了快速将某些东西扔在一起,不适合国际版发布,那​​就很好。

0

遵循指导原则(https://developer.android.com/guide/topics/resources/string-resource.html)。例如指定你的分数相关的字符串是这样的:

<string name="score">Score: %1$d</string> 

,并将其设置是这样的:

Resources res = getResources(); 
String text = res.getString(R.string.score, currentScore); 
textObjectScore.setText(text); 

这样你可以转换字符串,提供复数形式和格式的安全。