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

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

问题描述

我动态地创建了一个TextView,并希望将文本设置为可链接的。文字值为 Google 。我提到互联网&博客像这样,它显示了相同的方式,但我无法产生预期的结果。

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 并且没有链接。我也尝试了Clean project&再次建设,但没有成功。
我希望只看到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.

我的代码中缺少获取输出的内容?
BTW对于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
l.getLeftString() - 包含像Go to Google这样的网址文本

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

结果:
在对话框中输入Go to Google蓝色和下划线,并点击它浏览器打开并shwows各自的页面。在返回/退出浏览器时,它会从已经离开的状态再次进入应用程序。

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;设置链接在TextView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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