换行符 \n 在 textView Android 中无法正确显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3586763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
New Line character \n not displaying properly in textView Android
提问by Mike
I know that if you do something like
我知道如果你做类似的事情
myTextView.setText("This is on first line \n This is on second line");
Then it will display properly like this:
然后它会像这样正确显示:
This is on first line
This is on second line
这是在第一行
这是在第二行
When I store that string in a database and then set it to the view it displays as such:
当我将该字符串存储在数据库中,然后将其设置为它显示的视图时:
This is on first line \n This is on second line
这是在第一行 \n 这是在第二行
Here is the line of code I use to extract the string from the database:
这是我用来从数据库中提取字符串的代码行:
factView.setText(factsCursor.getString(MyDBAdapter.FACT_COLUMN));
I simply populate the database from a text file where each line is a new entry into the table so a line would look like this "This is on first line \n This is on second line" and it is stored as text.
我只是从一个文本文件中填充数据库,其中每一行都是表中的一个新条目,因此一行看起来像这样“这是在第一行 \n 这是在第二行”,并且它被存储为文本。
Is there a reason that it isn't displaying the \n characters properly? It must be something to do with the string being in the database. Any suggestions?
是否有原因没有正确显示 \n 字符?它必须与数据库中的字符串有关。有什么建议?
采纳答案by Austyn Mahoney
As Falmarri said in his comment, your string is being escaped when it is put into the database. You could try and unescape the string by calling String s = unescape(stringFromDatabase)
before you place it in your TextView
.
正如 Falmarri 在他的评论中所说,您的字符串在放入数据库时会被转义。您可以在将字符串String s = unescape(stringFromDatabase)
放入TextView
.
As a side note, make sure you are using DatabaseUtils.sqlEscapeString()
on any kind of data that is from the user or an unknown changeable source when inserting data into the database. This will protect you from errors and SQL Injection.
作为旁注,DatabaseUtils.sqlEscapeString()
在将数据插入数据库时,请确保您使用的是来自用户或未知可变来源的任何类型的数据。这将保护您免受错误和SQL 注入的影响。
回答by Blundell
I found this question Austyn Mahoney's answer is correct but here's a little help:
我发现这个问题 Austyn Mahoney 的回答是正确的,但这里有一些帮助:
private String unescape(String description) {
return description.replaceAll("\\n", "\\n");
}
description
being the string coming out of your SQLite DB
description
是从你的 SQLite 数据库出来的字符串
回答by Brendan Draper
"This is on first line"||x'0A'||"This is on second line"
The ||
concatenates strings and the x'0A'
is an unescaped newline.
在||
符连接字符串和x'0A'
是一个转义换行符。
If you're inserting records you'll have to replace every newline with "||x'0A'||"
(If your string is double quoted). This may seem clumsy compared to the other asnswers. However if your lines are in separate columns this also works in a select:
如果您要插入记录,则必须将每个换行符替换为"||x'0A'||"
(如果您的字符串是双引号)。与其他答案相比,这似乎很笨拙。但是,如果您的行位于单独的列中,这也适用于选择:
SELECT firstline||x'0A'||secondline FROM wherever;
I found this while having the same problem you are: http://www.mail-archive.com/[email protected]/msg43557.html
我在遇到与您相同的问题时发现了这一点:http: //www.mail-archive.com/[email protected]/msg43557.html
回答by Rohan K
Try \\n
instead of \n
. If it throws an exception than use newline keyword in place of \n....newline is one character, ascii 10; it's often entered in a string literal...and will serve your purpose....:)
尝试\\n
代替\n
. 如果它抛出异常而不是使用 newline 关键字代替 \n....newline 是一个字符,ascii 10; 它通常以字符串文字形式输入...并且可以满足您的目的....:)
回答by Mostowski Collapse
A text area can be in multi line or single line mode. When it is in single line mode newline characters '\n' will be treated as spaces. When in doubt, to switch multi line mode on you can use the following code:
文本区域可以是多行或单行模式。当它处于单行模式时,换行符 '\n' 将被视为空格。如有疑问,要打开多行模式,您可以使用以下代码:
setInputType(getInputType() | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
I had the problem that the same code did not work on honeycomb and on froyo, which seem to have different defaults. I am now also excluding the flag when I want to force a field to be single lined.
我遇到的问题是相同的代码在蜂窝和 froyo 上不起作用,它们似乎具有不同的默认值。当我想强制一个字段为单行时,我现在也排除了标志。
From the Android doc:
从 Android 文档:
public static final int TYPE_TEXT_FLAG_MULTI_LINE Added in API level 3
Flag for TYPE_CLASS_TEXT: multiple lines of text can be entered into the field. If this flag is not set, the text field will be constrained to a single line. Constant Value: 131072 (0x00020000)
public static final int TYPE_TEXT_FLAG_MULTI_LINE 在 API 级别 3 中添加
TYPE_CLASS_TEXT 标志:可以在字段中输入多行文本。如果未设置此标志,则文本字段将被限制为一行。常数值:131072 (0x00020000)
http://developer.android.com/reference/android/text/InputType.html#TYPE_TEXT_FLAG_MULTI_LINE
http://developer.android.com/reference/android/text/InputType.html#TYPE_TEXT_FLAG_MULTI_LINE
You have to set the flag before you populate the field.
您必须在填充该字段之前设置标志。