如何测试一个TextView是否包含特定链接?

huangapple go评论90阅读模式
英文:

How to test if a textview contains a specific link?

问题

我有一个包含链接的文本视图,链接是用a标签包含的。在string.xml文件中,我有一个类似这样的字符串 - This is my text view with <a href="url">link</a>

我想要通过编程方式检查文本视图是否具有此链接,并且该链接是否可点击。如果我尝试使用textview.getText().toString(),它会将HTML转换为纯文本(而不是原始HTML),并且会移除所有链接。

有没有办法可以进行检查?第二部分是,我可以手动验证链接是否可点击,但我想通过编程方式验证链接是否有效。我该如何做到这一点?

英文:

I have a textview with link included as an a tag. In string.xml I have string like this - This is my text view with <a href="url">link</a>

I want to programmatically check whether textview has this link and whether it is clickable. If I try using textview.getText().toString() it will convert html to plain text (not raw html) and remove all the links.

Is there any way I can check this? The second part is, I can manually verify that the link is clickable, but I want to programmatically verify whether the link is working. How do I do that?

答案1

得分: 1

感谢 @Andy

> textview.getUrls() 可以获取文本视图中的可跨度链接,因此在您的示例中,它将返回长度为1的数组,[0].getUrl() 将返回 "url"。

((TextView) v).getUrls()[0].getURL()
英文:

Thanks to @Andy

> textview.getUrls() gets you the spannable URLs in the text view - so
> in your example it would return an array of length 1 and [0].getUrl()
> would return "url".

((TextView) v).getUrls()[0].getURL()

huangapple
  • 本文由 发表于 2020年7月27日 03:20:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63104636.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定