我有以下TextView定义:
<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"/>
其中@string/txtCredits是一个字符串资源,包含<a href="some site">链接文本</a>。
Android正在突出显示TextView中的链接,但它们不响应单击。我做错了什么?我必须在我的活动中为TextView设置一个onClickListener像这样简单吗?
它看起来和我定义字符串资源的方式有关。
这行不通:
<string name="txtCredits"><a href="http://www.google.com">Google</a></string>
但这确实是:
<string name="txtCredits">www.google.com</string>
这是一个遗憾,因为我宁愿显示一个文本链接,而不是显示完整的URL。
我注意到使用android:autoLink="web"因此
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="web"/>
对于url来说工作得很好,但是因为我有一个电子邮件地址和电话号码,我想要链接,我最终使用这一行android:autoLink="all"像这样
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="all"/>
而且效果很好。
我注意到使用android:autoLink="web"因此
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="web"/>
对于url来说工作得很好,但是因为我有一个电子邮件地址和电话号码,我想要链接,我最终使用这一行android:autoLink="all"像这样
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="all"/>
而且效果很好。
在花了一段时间之后,我发现:
android:autoLink="web"工作,如果你有完整的链接在你的HTML。以下内容将以蓝色高亮显示,并可点击:
Some text <a href="http://www.google.com">http://www.google.com</a> .
一些文字http://www.google.com
view.setMovementMethod (LinkMovementMethod.getInstance ());将与以下工作(将突出显示和可点击):
Some text <a href="http://www.google.com">http://www.google.com</a> .
一些文字http://www.google.com
一些文本<a href="http://www.google.com">转到谷歌</a>
注意,第三个选项有一个超链接,但是链接的描述(标签之间的部分)本身并不是链接。android:autoLink="web"不工作与这样的链接。
android:autoLink="web"如果在XML中设置将覆盖view.setMovementMethod(LinkMovementMethod.getInstance());(即。第三类链接将被突出显示,但不能点击)。
这个故事的寓意是使用view.setMovementMethod(LinkMovementMethod.getInstance());在你的代码中,确保你没有android:autoLink="web"在你的XML布局中,如果你想要所有的链接都是可点击的。
[预棒棒糖测试以及棒棒糖及以上测试]
您可以从后端或资源文件中获取HTML字符串。
如果你把文本作为资源字符串,请确保添加CDATA标签:
<string name="your_text">![CDATA[...<a href="your_link">Link Title</a> ...]]</string>
然后在代码中,你需要获取字符串并将其赋值为HTML,并设置一个链接移动方法:
String yourText = getString(R.string.your_text);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
textView.setText(Html.fromHtml(yourText, Html.FROM_HTML_MODE_COMPACT));
} else {
textView.setText(Html.fromHtml(yourText));
}
try {
subtext.setMovementMethod(LinkMovementMethod.getInstance());
} catch (Exception e) {
//This code seems to crash in some Samsung devices.
//You can handle this edge case base on your needs.
}
我添加了这一行到TextView: android:autoLink="web"
下面是布局文件中的一个使用示例。
layout.xml
<TextView
android:id="@+id/txtLostpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:autoLink="email"
android:gravity="center"
android:padding="20px"
android:text="@string/lostpassword"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/txtDefaultpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:autoLink="web"
android:gravity="center"
android:padding="20px"
android:text="@string/defaultpassword"
android:textAppearance="?android:attr/textAppearanceSmall" />
string.xml
<string name="lostpassword">If you lost your password please contact <a href="mailto:support@cleverfinger.com.au?Subject=Lost%20Password" target="_top">support@cleverfinger.com.au</a></string>
<string name="defaultpassword">User Guide <a href="http://www.cleverfinger.com.au/user-guide/">http://www.cleverfinger.com.au/user-guide/</a></string>