2013-10-01 30 views
0

在做了很多教程之後,我正在玩Android。Strings.xml和附加

我有我想要打印,我已經寫在strings.xml檔案文本的各種線路一個TextView:

<string name="welcome">Welcome! What is your name?</string> 
<string name="welcome2">Welcome!</string> 

在我的主,我有:

gamehistory = (TextView)findViewById(R.id.textView2); 
gamehistory.setText(R.string.welcome); 

這工作正常,並顯示文字。我想將文本框用作「日誌」,以便稍後在我的代碼中打印下一行的welcome2。

gamehistory.append("\n" + R.string.welcome2); 

不幸的是,當我使用append時,它將字符串變成數字。

有沒有辦法避免這種情況?

+1

R.string.welcome2是整數資源字符串ID。你應該得到字符串本身。 getReources()。的getString(R.string.welcome2) – Leonidos

回答

0

要追加,我認爲不會有,但肯定的,你可以連接這樣

String outStr = getString(R.string.first) + 
    " " + getString(R.string.second); 

做參考Link to refrence

0
String welcomestr = Context.getResources().getString(R.string.welcome2) 
    gamehistory = (TextView)findViewById(R.id.textView2); 
    gamehistory.setText(welcomestr); 
0

使用Context.getResources().getString(id)了點。

getReources().getString(R.string.welcome2); // Since you're calling this in your activity itself. 

因爲R.string.welcome2是您的字符串資源的自動生成的整數值。

0

嘗試添加回車。

gamehistory.append("\r\n" + R.string.welcome2); 

或者將這些行添加到XML的TextView的一部分:

android:maxLines="2" 
android:minLines="1"