I am playing a bit around with Android after doing a number of tutorials.

I have a textview in which I want to print various lines of text that I have written in the strings.xml file:

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

In my main I have:

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

This works fine and it displays the text. I want to use the textbox as a "log" so that a bit later in my code it prints welcome2 in the next line.

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

Unfortunately, when I use append, it turns the string into a number.

Is there a way to avoid this?

有帮助吗?

解决方案

To append i think will not be there but yes you can concatenate like this

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

For Refrence Link to refrence

其他提示

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

Use the Context.getResources().getString(id) for that.

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

because R.string.welcome2 is an auto generated integer value for your string resource.

Try adding carriage return.

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

Or add these lines to TextView part of xml:

android:maxLines="2"
android:minLines="1"
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top