android: 使用 <a href> 设置链接在文本视图中 [英] android: Set link with <a href> in TextView

查看:24
本文介绍了android: 使用 <a href> 设置链接在文本视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我动态创建一个 TextView 并希望将文本设置为可链接.文本值为Google".我提到了互联网和博客 like this, 显示方式相同,但我不能'不会产生预期的结果.

I create a TextView dynamically and want to set the text as linkable. Text value is "Google". I referred to internet & blogs like this, which shows the same way, but I couldn't produce the expected results.

我尝试了不同的方法,但我看到的输出是只有文本的整个文本.我试过的代码是:

I tried different ways, but the output I see is the whole text with text only. The code I have tried with is :

TextView tv1 = new TextView(this);
tv1.setLayoutParams(textOutLayoutParams);
// Make Linkable
tv1.setMovementMethod(LinkMovementMethod.getInstance());
tv1.setText(Html.fromHtml(l.getLeftString()));

/*SpannableString s = new SpannableString(l.getLeftString());
Linkify.addLinks(s, Linkify.WEB_URLS);
tv1.setText(s);                 
tv1.setMovementMethod(LinkMovementMethod.getInstance());
*/
dialogLayout.addView(tv1);

在我的输出中,我看到Google"并且没有链接.我也尝试过清洁项目和再次构建它,但没有成功.我希望只看到带有蓝色下划线的Google"(默认情况下),点击 Google 时,浏览器会打开 http://google.com.

In my output I see "Google" and no link. I also tried Clean project & building it again, but no success. I am looking to see only "Google" as underlined with blue color (as default) and on clicking Google, the browser open with http://google.com.

我的代码中缺少什么来获取输出?顺便说一句,对于 REF:我使用 64 位 Win 7、Java、Eclipse、Android API 8-2.2

What is lacking in my code to get the output ? BTW For REF : I use 64bit Win 7, Java, Eclipse, Android API 8-2.2

非常感谢任何帮助.

推荐答案

我终于用下面的代码让它工作了:

I finally got it working using the following code :

TextView tv1 = new TextView(this);
tv1.setLayoutParams(textOutLayoutParams);
tv1.setText(Html.fromHtml("<a href=""+ l.getRightString() + "">" + l.getLeftString() + "</a>"));
tv1.setClickable(true);
tv1.setMovementMethod (LinkMovementMethod.getInstance());
dialogLayout.addView(tv1);

l.getRightString() - 包含类似 http:www.google.com 的 URLl.getLeftString() - 包含 URL 的文本,例如Go to Google"

l.getRightString() - contains a url like http:www.google.com l.getLeftString() - contains text for the url like "Go to Google"

结果:在我的对话框上用蓝色和带下划线的文本转到 Google",单击它会打开浏览器并显示相应的页面.在返回/退出浏览器时,它会再次从它离开的状态进入应用程序.

RESULTS : Text "Go to Google" on my dialog with blue color and underlined, and on clicking it the browser opens up and shwows the respective page. On returning/Exiting the browser it again comes to the app from the state where it had left.

希望这会有所帮助.

这篇关于android: 使用 &lt;a href&gt; 设置链接在文本视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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