2011-12-14 58 views
4

我需要在警報對話框中顯示多行消息,而不僅僅是一個段落。在警報對話框中顯示多行文本

new AlertDialog.Builder(this) 
.setTitle("Place") 
.setMessage("Go there" + 
     "Go here") 
.setNeutralButton("Go Back", null) 
.show(); 

有沒有辦法以新行開始?就像在Microsoft Word中輸入一個句子後輸入一樣?

回答

12

這個不能保證,但通常做多行,你做這樣的事情:

.setMessage("Go there\nGo here"); 

「\ n」是一個轉義字符,意思是「新線」,我不知道您的具體情況,但您可以在幾乎所有的AFAIK中使用它。

0

您是否嘗試過回車或換行符?這些分別是人物10和13(特殊字符\ n和\ r或\ u000a和\ u000d)

+0

詳細說明,典型環境中的換行符是CR/LF或字符13,後跟字符10(\ r \ n)。 – 2011-12-14 20:37:24

+0

謝謝!!!!!!!!! – 2011-12-14 20:40:55

4

使用「\ n」的標籤在你的字符串:

new AlertDialog.Builder(this) 
    .setTitle("Place") 
    .setMessage("Go there" + "\n" + 
      "Go here") 
    .setNeutralButton("Go Back", null) 
    .show(); 
0

我認爲最好的辦法是避免斷線和使用自定義視圖組件。

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
TextView tw =new TextView(this); 
     tw.setMaxLines(10); 
     tw.setPadding(3,3,3,3); 
     tw.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); 
     tw.setSingleLine(false); 
     tw.setText("ver long messagge \n with break line \n"); 
     builder.setView(tw); 

有了這個解決方案可以打破,如果你想使用\n行,但如果你不這樣做,文字將多條線路被包裹(因爲我設置tw.setSingleLine(false);)。