简体   繁体   English

从字符串中删除 HTML 并在脚本标签内显示要点 - Android

[英]Remove HTML from a String and showing the Gists inside the script tags - Android

Trying to display the Github Gists from a String in a TextView.尝试从 TextView 中的字符串显示 Github Gists。 When I clean the String from the HTML with 'Html.fromHtml()' -->当我使用 'Html.fromHtml()' 从 HTML 中清除字符串时 -->

 <p>Lorem ipsum dolor sit <a href="http://Lorem ipsum.com/Lorem ipsum dolor sit amet">amet</a>,</p> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam ultrices lorem scelerisque year&#8217;s Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam ultrices lorem scelerisque<br /> </p> <p>Interdum et malesuada fames ac ante ipsum primis in faucibus</p> <p><script src="https://gist.github.com/LoremIpsum/d3c4dff75f555a54aa0f.js"></script>Donec ac ultrices elit. Curabitur elementum pulvinar augue a congue. Phasellus in quam ligula.<script src="https://gist.github.com/dolorSitAmet/53ae35ddd74bb0837c12.js"></script></p>
* I get the same result as when running the code snippet. *我得到与运行代码片段时相同的结果。

How can I show the Gists like in the picture below:如何显示如下图所示的要点: 在此处输入图片说明

As the official documentation tells us, the Html.fromHtml() method正如官方文档告诉我们的, Html.fromHtml()方法

Returns displayable styled text from the provided HTML string从提供的 HTML 字符串返回可显示的样式文本

Unfortunately, the reference is a bit vague about which HMTL tags are supported.不幸的是,参考文献对支持哪些 HMTL 标签有点含糊。 I searched around a bit, and the source code at grepcode.com shows that the <script> tag is not evaluated.我搜索了一下, grepcode.com 上源代码显示<script>标签没有被评估。

So the answer to your question is: with a TextView , you can't.所以你的问题的答案是:使用TextView ,你不能。 Use a WebView instead.改用WebView Click this link for a short introduction.单击此链接以获取简短介绍。

textView.setText(Html.fromHtml(htmlCode));

它会做你的工作。

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

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