Displaying multiple lines of text and variables in

2019-03-11 00:33发布

问题:

I need to display multiple lines of text in an Alert Dialog. If I use multiple setMessage() methods, only the last setMessage is displayed, as shown below.

final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
                alertDialog.setTitle("Statistics:");
                alertDialog.setMessage("No. of attempts: " + counter);
                alertDialog.setMessage("No. of wins: " + counterpos);
                alertDialog.setMessage("No. of losses: " + counterneg);

Is there a way to create a new line for each of these in the dialog? Like using \n in System.print.out(); method.

Thanks!

回答1:

You can do something like this

String alert1 = "No. of attempts: " + counter;
String alert2 = "No. of wins: " + counterpos;
String alert3 = "No. of losses: " + counterneg;
alertDialog.setMessage(alert1 +"\n"+ alert2 +"\n"+ alert3);   


回答2:

You could just create one string of everything you want to show and add "\n" where you'd like the line breaks to be.

alertDialog.setMessage("No. of attempts: " + counter + "\n" + 
            "No. of wins: " + counterpos + "\n" + 
            "No. of losses: " + counterneg);

Or even better to use a StringBuilder:

StringBuilder sb = new StringBuilder();
sb.append("No. of attempts: " + counter);
sb.append("\n");
sb.append("No. of wins: " + counterpos);
sb.append("\n");
sb.append("No. of losses: " + counterneg);
alertDialog.setMessage(sb.toString());

And the best way to do it would be to extract the static texts into a string resource (in the strings.xml file). Use the %d (or %s if you want to insert strings rather than ints) to get the dynamic values in the right places:

<string name="alert_message">No. of attempts: %1$d\nNo. of wins: %2$d\nNo. of losses: %3$d</string>

And then in code:

String message = getString(R.string.alert_message, counter, counterpos, counterneg);
alertDialog.setMessage(message);


回答3:

You can also insert newlines directly in the strings.xml file:

<string name="my_string_text">This would revert your progress.\n\n Are you sure you want to proceed?</string>



回答4:

Kotlin simplifies the solution by:

  • chaining the calls of the set methods
  • using interpolated strings

as follows:

"AlertDialog.Builder This Builder object to allow for chaining of calls to set methods " (https://developer.android.com/reference/android/app/AlertDialog.Builder)

    fun alertDemo() {
        var counter: Int = 5

        var counterpos: Int = 2
        var counterneg: Int = 3
        val builder = AlertDialog.Builder(this)
            .setTitle("Statistics:")
            .setMessage("""
                |number of
                |
                |attempts: $counter
                |wins: $counterpos
                |losses: $counterneg
                """.trimMargin())
            .show()
    }

I had prepared a screen-shot of the result, but as I am new here, I appear to have learned that uploading screenshots may be restricted to higher level community peers. Or did I miss something? Thank you for enlighting (perhaps not only) myself :)

The screenshot comes in a nice format without showing the bars.

PS: For the minimalists, due to chaining, we could even eliminate the redundant "val builder ="