英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论