【发布时间】:2015-02-22 18:40:53
【问题描述】:
在我的新闻应用程序中,我从 JSON 中获取需要在 TextView 中显示的数据。 但是进来的数据内容是和html标签混在一起的。 我尝试从字符串中删除 html 标签,但遇到了一些问题。
这是我拥有的字符串:
String testString = "<a href=\"#"><img src=\"xyz.jpg" width=\"196\" height=\"300\" /></a>Residents of Chisapani bazaar in the south of Khotang district have been living in darkness for the past three years.\r\n\r\nThe power house of Dobhane river Hydropower Project was damaged in a landslide three years ago.\r\n\r\n Source:ABCD";
我需要在 TextView 中显示此字符串,不包括 html 标记。这就是我所做的。
textbox = (TextView) findViewById(R.id.textview1);
textbox.setText(Html.fromHtml(testString).toString());
这样做时,甚至省略了 \n (换行符)部分,这将整个文本变成了一个单独的块,删除了段落。但这不是首选。我需要有条理的新闻内容文本。
有没有什么方法可以只删除<a> 或<img> 等标签但恢复\n。
【问题讨论】:
-
您是否尝试过将
\r\n的出现转换为<br />? first SO result, second SO result 用于“Html.fromHtml 换行”