简体   繁体   English

Java \\ n行分隔符不起作用

[英]Java \n line separator not working

I am creating an application that has a data log. 我正在创建一个具有数据日志的应用程序。 I build up this data with a string that I add to everytime the application receives data and then display it in an edittext box. 我使用字符串构建此数据,每次应用程序接收数据时我都会添加该字符串,然后将其显示在edittext框中。

The problem I am having is that it will display the data but without the new lines. 我遇到的问题是它将显示数据但没有新行。

A basic example of the code: 代码的基本示例:

String logText;
EditText etLog;

logText = data + "\n" + logText;
etLog.setText(logText);

I know that I can use nl = System.getProperty("line.separator"); 我知道我可以使用nl = System.getProperty("line.separator"); to get a new line but I'm puzzled as to why "\\n" is not working when I've used it in different apps I've created before. 得到一个新的行,但我很困惑为什么“\\ n”在我之前创建的不同应用程序中使用它时无效。

Not overly keen on answering my own question but I solved it by changing the inputType of my EditText to textMultiLine in my xml code. 不是过于热衷于回答我自己的问题,但我通过在我的xml代码中将EditText的inputType更改为textMultiLine来解决它。
xml code: xml代码:

android:inputType="textMultiLine"


Allows the EditText to make use of "\\n" within a string. 允许EditText在字符串中使用“\\ n”。

Usually the problem arises because of a particular programs preferences. 通常由于特定的程序偏好而产生问题。 The \\n (new line feed) is one possibility, but there is also \\r aka carriage return. \\n (新换行符)是一种可能性,但也有\\r也称回车。

The two are essentially the same, just that there's a standardisation issue. 这两者基本相同,只是存在标准化问题。 Java uses both for compatibility. Java使用两者来兼容。 Try one, the other, both. 尝试一个,另一个,两个。 Usually one will work. 通常一个人会工作。

For editText, I believe its \\r\\n . 对于editText,我相信它\\r\\n

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM