首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Android警报对话框中使用HTML

在Android警报对话框中使用HTML
EN

Stack Overflow用户
提问于 2013-02-02 02:37:32
回答 6查看 22K关注 0票数 26

我有一些信息要显示在对话框中。它类似于Title,然后是text;Title,然后是text。像wise一样,有4个标题和4个描述要显示。它应该是这样的

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

正如你所看到的,有粗体文本,带下划线的文本,换行符等。我想将这种文本添加到警告框中,所以下面是我尝试的内容。

代码语言:javascript
复制
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

然而,这个技巧并不起作用。实际情况是,所有的HTML标记都是原样显示的!文本不清晰!看起来像是与AlertBox的默认颜色黑色的背景混合在一起。我该如何解决这个问题?请帮帮我!

PS:还是我用错了方法?错误的对话框?

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2013-02-02 02:40:43

您需要在TextView中使用Html.fromHtml()标记,如下所示:

代码语言:javascript
复制
msg.setText(Html.fromHtml("<u>Message</u>"))

您还可以看到所有的HTML tags supported by TextView

票数 61
EN

Stack Overflow用户

发布于 2015-07-30 03:18:28

事实证明,您实际上不需要任何额外的TextViews来做到这一点。只需在警报的"setMessage()“调用(它替换了问题中的"setView()”调用)中包含HTML,并将html格式的字符串传递给它。但是,请确保在格式化中仅使用<b><u><i>,因为它仅支持这些标记。但是,如果您对警报中的文本使用字符串资源,请调用getResources().getText(R.id.yourHtmlString)而不是getResources().getString(R.id.yourHtmlString),否则标记将完全从字符串中剥离。

票数 16
EN

Stack Overflow用户

发布于 2013-02-02 12:19:10

如果要添加链接并使其可单击,

代码语言:javascript
复制
msg.setMovementMethod(LinkMovementMethod.getInstance());
msg.setClickable(true);
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14652894

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档