我如何在一个TextView可点击的链接? [英] How do I make links in a TextView clickable?
问题描述
我有以下TextView中定义的:
I have the following TextView defined:
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/txtCredits"
android:autoLink="web" android:id="@+id/infoTxtCredits"
android:layout_centerInParent="true"
android:linksClickable="true"></TextView>
其中, @字符串/ txtCredits
是包含℃的字符串资源; A HREF =一些网站的&GT;链接文字&lt; / A&GT;
。
机器人是突出在TextView的链接,但它们不以点击响应。谁能告诉我什么,我做错了吗?我一定要的东西这么简单设置onClickListener的TextView的在我的活动?
Android is highlighting the links in the TextView, but they do not respond to clicks. Can someone tell me what I'm doing wrong? Do I have to set an onClickListener for the TextView in my activity for something as simple as this?
看起来它必须与我定义我的字符串资源的方式。 这不能正常工作:
Looks like it has to do with the way I define my string resource. This does not work:
<string name="txtCredits"><a href="http://www.google.com">Google</a></string>
但这:
<string name="txtCredits">www.google.com</string>
这是一个坏消息,因为我宁愿显示一个文本链接不是显示完整的URL。
Which is a bummer because I would much rather show a text link than show the full URL.
推荐答案
埋在API演示我找到了解决我的问题:
Buried in the API demos I found the solution to my problem:
Link.java:
Link.java:
// text2 has links specified by putting <a> tags in the string
// resource. By default these links will appear but not
// respond to user input. To make them active, you need to
// call setMovementMethod() on the TextView object.
TextView t2 = (TextView) findViewById(R.id.text2);
t2.setMovementMethod(LinkMovementMethod.getInstance());
我删除了大部分在我的TextView属性以匹配在演示。
I removed most of the attributes on my TextView to match what was in the demo.
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imgCredits"
android:layout_centerInParent="true"
android:layout_marginTop="20dp"
android:text="@string/txtCredits"/>
这解决了这个问题。 pretty的难以发现和修复。
That solved it. Pretty difficult to uncover and fix.
这篇关于我如何在一个TextView可点击的链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!