Android 中的超链接
我有一个问题:如何在普通文本的文本块中放置超链接?我有一些文本,在文本中我提到了一个 URL,我希望该 URL 具有不同的颜色、下划线且可点击。
我知道android中的超链接可以用“Linkify”放置.. 我已经提到了 android docs 页面
考虑相同上面带有“android docs”的段落我想在android中显示......
I have a question: How can I place a hyperlink WITHIN a text-block of normal text? I have some text and in the text I mention a URL and I want this URL to be in a different color, underlined and click-able.
I know hyperlinks in android can be placed with "Linkify"..
and i have referred android docs page
Consider the same above paragraph with "android docs" i want to display in android.....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
@费多尔....
我发现提供了来自 的链接API 演示。
这种方法最终用于在段落文本之间提供链接。
@Fedor....
i have found to give links from API Demos .
this approach is finally be used for giving link in between text of paragraph.
看看 TextView 下划线电话号码和超链接
但是我发现 Linkify 没有为您提供您需要的功能。没问题,您可以手动实现。只需添加 ClickableSpan 并处理单击即可。代码示例位于 http://www.orangeapple.org/?p=354。
Take a look at that TextView underline phone number and hyperlink
But I can see Linkify doesn't provide you the functionality you need. Not a problem, you can implement it manually. Just add ClickableSpans and handlre click. Code sample here http://www.orangeapple.org/?p=354.
我知道这是一个老问题,但这是我最近遇到的一个问题,而且我没有找到对我有帮助的 StackOverflow 答案。因此,经过大量的实验,我找到了一个解决方案,并想将其分享给后代。
在 strings.xml 中,使用 HTML 标签和标记:
在 XML 中,确保不添加任何特殊属性。不要使用 android:autolink
在 Java 代码中,必须使用 fromHtml、Linkify 和 LinkMovementMethod。确保在 LinkMovementMethod 之前调用 Linkify
另一个警告:如果您使用这些 HTML 链接,其中链接的显示文本与 URL(Google 和 www.google.com)不匹配,请勿将原始 URL 放入字符串中。所以:
会将“www.google.com”转换为链接,但不会将“android docs”转换为链接。
这对我来说是非常令人沮丧的,因为我尝试了许多不同的组合并遵循了许多在线示例,但在我找到上述正确的神奇组合之前,这些组合都不起作用。希望它对其他人有帮助!
I know this is an old question, but this is a problem I recently struggled with, and I found no StackOverflow answer that helped me. So after extensive experimentation, I found a solution and thought I would share it for posterity.
In your strings.xml, use HTML tags and the markers:
In your XML, make sure to NOT add any special attributes. Do NOT use android:autolink
In your Java code, you must use fromHtml, Linkify, and LinkMovementMethod. Make sure to call Linkify before LinkMovementMethod
Another caveat: if you are using these HTML links, where a link has display text that does not match the URL (Google and www.google.com), do NOT put raw URLs in the string. So:
Will turn "www.google.com" into a link, but will not turn "android docs" into a link.
This was very frustrating for me to figure out, as I tried many different combinations and followed many online examples, none of which worked until I found just the right magic combination described above. Hope it helps someone else!
使用 Spannable 字符串
Use Spannable String