Displaying multiple lines of text in an Alert Dial

2019-02-17 11:20发布

I need to display multiple lines of messages, rather than just one paragraph, in an alert dialogue.

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

Is there a way to start at new lines? Just like hitting enter after a sentence in Microsoft Word?

4条回答
甜甜的少女心
2楼-- · 2019-02-17 11:58

Use the "\n" tag in your strings:

new AlertDialog.Builder(this)
    .setTitle("Place")
    .setMessage("Go there" + "\n" +
            "Go here")
    .setNeutralButton("Go Back", null)
    .show();
查看更多
乱世女痞
3楼-- · 2019-02-17 12:00

Have you tried a carriage return or line feed character? These are characters 10 and 13 respectively (special characters \n and \r, or \u000a and \u000d)

查看更多
够拽才男人
4楼-- · 2019-02-17 12:11

I think the best solution is to avoid break line and use a custom view component.

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);

With this solution you can break the line if you want using \n, but if you don't, the text will be wrapped on multiple lines (since I set tw.setSingleLine(false);).

查看更多
小情绪 Triste *
5楼-- · 2019-02-17 12:20

No guarantees on this, but usually to do multiple lines you do something like:

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

The "\n" is an escape character that means "New Line" I don't know about your specific case, but you can use it in just about everything AFAIK.

查看更多
登录 后发表回答