在android中制作超链接textview [英] Make a hyperlink textview in android

查看:27
本文介绍了在android中制作超链接textview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为诸如 Google 之类的 textview 文本制作链接.反正有没有这样的链接.(i.e) 当点击谷歌这个词时,它应该打开相应的链接.欢迎提出任何想法.

I want to make a link for a textview text like Google. Is there anyway to make link like this. (i.e) When clicking on the word Google it should open the appropriate link. Any ideas are welcome.

推荐答案

试试这个,让我知道会发生什么..

Try this, and let me know what happen..

使用java代码:

TextView textView =(TextView)findViewById(R.id.textView);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href='http://www.google.com'> Google </a>";
textView.setText(Html.fromHtml(text));

从 API 级别 >= 24 起 Html.fromHtml(String source) 已弃用,而是使用 fromHtml(String, int),

From API level >= 24 onwards Html.fromHtml(String source) is deprecated instead use fromHtml(String, int),

textView.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT));

或在布局 xml 文件中,在 TextView 小部件属性内

Or in layout xml file, inside your TextView widget attributes

android:autoLink="web"
android:linksClickable="true"

这篇关于在android中制作超链接textview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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