我有一个显示 html 的 TextView 。
> textview.setText(html.fromhtml(document))
> textview.setMovementMethod(LinkMovementMethod.getInstance());
当我点击某些链接时,有些链接会在浏览器中打开,有些则不会。
Logcat 是这样说的:
> No Activity found to handle Intent { act=android.intent.action.VIEW
> dat=law.fordham.edu/ (has extras) }
看起来是一个简单的修复。只需将“http://”添加到文档中的每个链接即可。
如果所有链接都以“www”开头,事情就会很容易。我会这样做:
> replaceAll("www", "http://www")
这确实适用于某些链接。但并非所有内容都以“www”开头,因此,我需要一种不同的方法来在它们前面添加 http://
最好的方法是什么?
编辑:
paragraphs = doc.select("div.sectionContent>*");
set = paragraphs.toString().replaceAll("\\\\n", "\n").trim();
links = doc.select("a");
for (Element i: links){
String relHref = i.attr("abs:href");
if (!relHref.contains("http")){
set = set.replace(relHref, "http://"+relHref);
}
}
我使用 jsoup 来完成这一切,但我不断得到:
java.lang.OutOfMemoryError
最佳答案
处理 HTML,查找 <a>
标签,然后如果 anchor 内的 url 开头不是您想要的,则进行调整。
正则表达式搜索应该能够为您找到 anchor 。
关于java - 在 TextView 中添加 http ://前面的所有可链接项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20767393/