我正在从 SQLite 数据库获取一些数据。数据由文本和一些制表符 (\t) 和换行符 (\n) 元素组成。当我从文件中打印字符串时,所有内容都会正确显示,但如果我从数据库中获取字符串,则换行符和制表符将以文本形式显示。
例如:
This is my first row\tText afterTab\nThis is my second row\tText after second tab
这是我的源代码:
java:
textfieldAntwort = (TextView) findViewById(R.id.textViewAntwort);
textfieldAntwort.setText(frageundantwort.getAntwort());
xml:
<TextView
android:id="@+id/textViewAntwort"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textViewFrage"
android:layout_below="@+id/ima_frage"
android:layout_marginTop="20dp"
android:text="@string/txt_answer" />
最佳答案
您需要对字符串进行转义
String s = unescape(stringFromDatabase)
在将其放入 TextView 之前。
private String unescape(String description) {
return description.replaceAll("\\\\n", "\\\n").replaceAll("\\\\t", "\\\t");
}
来源:New Line character \n not displaying properly in textView Android
关于android - 换行符和制表符未正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20848328/