2012-07-01 86 views
3

以下忽視,當我展示他們的新線如何從EditText保留換行符?

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

編輯:我的EditText已經設置,當我寫它它返回與蓋上面

EDIT2單行multilined文本但是多:我收到來自EDITTEXT文本和一個TextView顯示它

+0

你會發布更多的代碼?你有什麼不應該忽略新行字符。 – Sam

回答

18

要解決它使用replaceAll("\\n", "<br />")

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

上帝保佑你!謝謝,當我需要使用html.toHtml轉換消息時,我遇到了一個問題,我必須創建一個Spannable,但在這個過程中,
會丟失=/ – user2582318

+0

其感謝上帝保佑你! – sirmagid

+0

上帝保佑你!你節省了我的時間! – Mabad

1

嘗試添加android:inputType="textMultiLine"如果你是在一個TextView顯示它也增加 android:singleLine="false"到您的EditText定義。

編輯:

這是你的EditText應該有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" 
/> 
+0

看到我原來的文章我編輯它 – code511788465541441

+0

你寫它,並在TextView中顯示它?如果是這樣你的文本視圖應該有android:inputType =「textMultiLine」 –

+0

編輯我的回答 –

2

使用android:inputType="textMultiLine"屬性...設置它的XML或者Eclipse桂

+0

看到我原來的文章我編輯它 – code511788465541441

+0

你在哪裏設置文本...是它的另一個EditText或TextView –

-2
String text=editText1.getText().toString().replace("\n", " ").trim(); 
0

這也適用於:

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

試試這個,它應該工作

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