TextView中url的Android活动链接 [英] Android active link of url in TextView

查看:31
本文介绍了TextView中url的Android活动链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 Web 服务获取动态文本并在 TextView 中显示相同内容.有时 TextView 的 url 像 hello.我已使用以下代码设置文本.

I have getting dynamic text from a web service and showing the same in a TextView. Sometimes the TextView has url like <a href="http://hello.com">hello</a>. I have set the text using the following code.

textView.setText(Html.fromHtml(sampletext));

并在包含TextView的相应xml中设置android:autoLink="web".现在链接正确显示为蓝色和下划线,但我发现它只是一个死链接.如果我们尝试点击它,什么也不会发生.我必须做什么才能使链接处于活动状态?

And also set android:autoLink="web" in the corresponding xml of that contains the TextView. Now the link is showing properly with blue color and underline, but I found the its just a dead link. Nothing is happening if we try to click it. What I have to do to make the link active?

推荐答案

重温所有解决方案后,总结和一些解释:

After revisiting all solutions, a summary with some explanations:

android:autoLink="web" 

即使没有设置 android:linksClickable 也会找到一个 URL 并创建一个链接,链接默认是可点击的.您不必单独保留 URL,即使在文本中间,它也会被检测到并可点击.

will find an URL and create a link even if android:linksClickable is not set, links are by default clickable. You don't have to keep the URL alone, even in the middle of a text it will be detected and clickable.

<TextView
    android:text="My web site: www.stackoverflow.com"
    android:id="@+id/TextView1"
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content"
    android:autoLink="web">
</TextView>

通过代码设置链接,原理相同,布局中不需要pattern或android:autoLink,使用Linkify自动找到链接:

To set a link via the code, same principle, no need for pattern or android:autoLink in layout, the link is found automatically using Linkify:

  final TextView myClickableUrl = (TextView) findViewById(R.id.myClickableUrlTextView);
  myClickableUrl.setText("Click my web site: www.stackoverflow.com");
  Linkify.addLinks(myClickableUrl, Linkify.WEB_URLS);

这篇关于TextView中url的Android活动链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆