如何保留编辑文本中的换行符



以下显示新行时忽略新行

private void post() {
        String subject = "";
        String message = "";
        subject = etSubject.getText().toString();
        message = etMessage.getText().toString();
编辑

:我的编辑文本已经设置为多行,但是当我在其中写入多行文本时,它会作为单行返回,上面有封面

edit2:我正在从编辑文本中获取文本并将其显示在文本视图上

使用 replaceAll("\n", "<br />") 解决了它

message = etMessage.getText().toString().replaceAll("\n", "<br />")

使用 android:inputType="textMultiLine" 属性...将其设置为 xml 或 Eclipse Gui

尝试添加android:inputType="textMultiLine",如果要在文本视图中显示它,还要添加 android:singleLine="false"到您的编辑文本定义。

编辑:

您的编辑文本应该具有android:inputType="textMultiLine"例如:

<TextView
        android:id="@+id/myDisplayingTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
/>
<EditText
        android:id="@+id/myEditText"
        android:inputType="textMultiLine"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=""
       android:singleLine="false"
/>

这也适用于:

editText.getText().toString().replaceAll("\n", "n");

试试这个,它应该可以工作

message = etMessage.getText().toString().replaceAll("[rn]+", "n");

在 kotlin 中,试试这个, val msg = evMsg.text.toString().replace("\n".toRegex(), "<br />")

String text=editText1.getText().toString().replace("n", " ").trim();

最新更新