将下一行添加到要在文本视图中显示的字符串,android?

时间:2015-01-03 06:49:21

标签: java android xml string textview

我有来自服务器的XML格式的字符串值。

例如

<rss version="2.0" xmlns:Data="http://www.google.com">
<Data: Mesage="Dear Member,\\nWe wish you and your family a Very Happy New Year 2015./>
</rss?

其中\\ n表示新行。当我在服务器上的字符串中添加“\ n”时,它变为“\\ n”。

它在textview中显示为

Dear Member,\\nWe wish you and your family a Very Happy New Year 2015.

我将文字设为

textview.setText(data_message.replaceAll("\\n", "\n"));
textview.setText(data_message.replaceAll("\\n",System.getProperty("line.separator");

我试过了两个。但下一行不会出现。

如何添加下一行。

@almas

使用您的代码。我得到的输出是

Dear Member,\
We wish you and your family a Very Happy New Year 2015.

3 个答案:

答案 0 :(得分:1)

问题出在replaceAll方法的第一个参数中,它也需要转义。 这种结构很难看,但工作正确:

    String text = "Dear Member,\\nWe wish you and your family a Very Happy New Year 2015.";
    textView.setText(text.replaceAll("\\\\n", "\n"));

答案 1 :(得分:0)

尝试使用replace api,因为您没有使用正则表达式,如下所示:

String data_message = "Dear Member,\\nWe wish you and your family a Very Happy New Year 2015.";
System.out.println(data_message.replace("\\n", System.getProperty("line.separator")));
Output:
Dear Member,
We wish you and your family a Very Happy New Year 2015.

答案 2 :(得分:0)

你可以试试这个,

  String text = "Dear Member,\\nWe wish you and your family a Very Happy New Year 2015.";
  textView.setText(text.replaceAll("\\\n", "/n"));

  String text = "Dear Member,\\nWe wish you and your family a Very Happy New Year 2015.";
  textView.setText(text.replaceAll("\\\n",System.getProperty("line.separator"));