[英]Add next line to string to be displayed in text view, android?
我有來自服務器的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.
問題出在replaceAll方法的第一個參數中,該參數也需要轉義。 這種結構很難看,但是可以正確使用:
String text = "Dear Member,\\nWe wish you and your family a Very Happy New Year 2015.";
textView.setText(text.replaceAll("\\\\n", "\n"));
嘗試使用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.
你可以試試看
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"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.