简体   繁体   English

在Android警报对话框中使用HTML

[英]Using HTML in Android Alert Dialog

I have some amount of informations to be displayed in Dialog Box. 我在对话框中显示了一些信息。 It comes like Title, then under it text; 它就像Title,然后在它下面; Title, then under it text. 标题,然后在它下面的文字。 Like wise, there are 4 titles and 4 descriptions to be displayed. 同样明智的是,有4个标题和4个描述要显示。 It should come like this 它应该是这样的

Title One 标题一

description;description;description;description;description;description;description;description;description;description;description;description;description;description;description 描述;描述;描述;描述;描述;描述;描述;描述;描述;描述;描述;描述;描述;描述;描述

Title Two 标题二

description;description;description;description;description;description;description;description;description;description;description;description;description;description;description 描述;描述;描述;描述;描述;描述;描述;描述;描述;描述;描述;描述;描述;描述;描述

As you can see, there are bold texts, underlined texts, line breaks etc. I want to add this kind of a text to the alert box, so below is what I tried. 正如您所看到的,有大胆的文本,带下划线的文本,换行符等。我想将这种文本添加到警告框中,所以下面是我尝试过的。

TextView msg = new TextView(this);
msg.setText("<html><u>Message</u></html>")

AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setTitle("Title");
ab.setView(msg);
ab.setCancelable(false);

//rest of the code

However this trick didn't work. 然而,这个技巧不起作用。 What happened is, all the HTML tags showed up as they are! 发生的事情是,所有的HTML标签都显示出来了! And the text is not clear! 文字不清楚! Seems like it mixed with the background of the default colour of AlertBox, black. 好像它混合了AlertBox默认颜色的背景,黑色。 How can I solve this issue? 我该如何解决这个问题? Please help! 请帮忙!

PS: Or am I using the wrong method? PS:或者我使用了错误的方法? Wrong dialog box? 错误的对话框?

You will need to use Html.fromHtml() to use HTML tags in TextView as: 您需要使用Html.fromHtml()TextView使用HTML标记:

msg.setText(Html.fromHtml("<u>Message</u>"))

And you also see all HTML tags supported by TextView . 您还可以看到TextView支持的所有HTML标记

If you want to add a link and make it clickable, 如果要添加链接并使其可单击,

msg.setMovementMethod(LinkMovementMethod.getInstance());
msg.setClickable(true);

As it turns out, you don't actually need any extra TextViews to do this. 事实证明,您实际上并不需要任何额外的TextView来执行此操作。 Simply include the HTML in your alert's "setMessage()" call (which replaces the "setView()" call in your question) and pass it the html-formatted string. 只需在警报的“setMessage()”调用中包含HTML(它替换了您的问题中的“setView()”调用)并将其传递给html格式的字符串。 Be sure to only use <b> , <u> , and <i> in your formatting, though because those are the only tags it supports. 请务必仅在格式化中使用<b><u><i> ,因为这些是它支持的唯一标记。 If you're using a String resource for the text in your alert, call getResources().getText(R.id.yourHtmlString) rather than getResources().getString(R.id.yourHtmlString) , though, or the tags will be completely stripped from the String. 如果您正在使用字符串资源作为警报中的文本,请调用getResources().getText(R.id.yourHtmlString)而不是getResources().getString(R.id.yourHtmlString) ,或者标记将是从字符串中完全剥离。

Try this, Font color, 试试这个,字体颜色,

   String source = "<b><font color=#ff0000> Loading. Please wait..."
                + "</font></b>";

Font underline, 字体下划线,

   String source = <u>Message</u>

 msg.setText(Html.fromHtml(source));

If you need to add more complex HTML , with CSS and META , you can add a WebView to the dialog, like this: 如果需要使用CSSMETA添加更复杂的HTML ,可以在对话框中添加WebView ,如下所示:

String webViewString = yourMeta + yourCss + yourHtml;
yourCustomWebView.loadData(webViewString, "text/html; charset=UTF-8",
                    null);
yourAlertDialog.setView(yourCustomWebView);

This way, you can display fully formatted HTML pages in your dialog. 这样,您就可以在对话框中显示完全格式化的HTML页面。

In case if you need it. 万一你需要它。 Better to use HtmlCompat.fromHtml((htmlString, 0) for compatibility with older versions. 最好使用HtmlCompat.fromHtml((htmlString, 0)与旧版本兼容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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