简体   繁体   中英

Is it possible to change text colour of a string in Java?

I want to know if it is possible to change text colour within strings in Java?

I have a peice of text below within my alert dialog and basically what I like is for all the text that are in bold (), I want the colour of those text to be red whilst the rest of the text is black. Is this possible and if so how to implement?

    alertDialog.setMessage(Html.fromHtml("<b>Number of Human Players Required:</b>" + "<br/><br/>" +
 " 2" + "<br/><br/>" +
  "<b>Objective Of The Game:</b>" + "<br/><br/>" +
 "The objective of Tic Tac Toe is to get three in a row either in a vertical line, " +
                                    "horizontal line or diagonal line."

    )

Thanks,

You can do something like this:

String s =  "<font color='red'><b>Number of Human Players Required:</b></font>" 
            + "<br/><br/>" + " 2" + "<br/><br/>" 
            + "<font color='blue'><b>Objective Of The Game:</b></font>" 
            + "<br/><br/>" 
            + "<font color='yellow'>The objective of Tic Tac Toe is to get three in a row either in a vertical line, " 
            + "horizontal line or diagonal line.</font>"

alertDialog.setMessage(Html.fromHtml(s));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM