我有以下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。
我的代码是这样的:
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/link"
android:text="@string/forgot"
android:layout_marginTop="16dp"
android:gravity="center"
android:linksClickable="true"/>
我的Java代码是这样的:
/*TextView action*/
TextView textView = (TextView) findViewById(R.id.link);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(LoginActivity.this,forgot.class));
}
});
这只是将链接指向另一个活动。但这个链接是可点击的,而且工作流畅。在Android Studio 1.5中测试(预览)
你可以简单地添加链接到你的TextView与Android的Linkify库。
添加到你的strings.xml
<string name="text_legal_notice">By continuing, you confirm that you have read, understood and agreed to our %1$s and %2$s.</string>
<string name="text_terms_conditions">Terms & Conditions</string>
<string name="text_privacy_policy">Privacy Policy</string>
加入到你的活动中
final String termsConditionsText = getString(R.string.text_terms_conditions);
final String privacyPolicyText = getString(R.string.text_privacy_policy);
final String legalText = getString(
R.string.text_legal_notice,
termsConditionsText,
privacyPolicyText
);
viewBinding.textViewLegalNotice.setText(legalText);
Linkify.addLinks(
viewBinding.textViewLegalNotice,
Pattern.compile(termsConditionsText),
null,
null,
(match, url) -> "https://policies.google.com/terms"
);
Linkify.addLinks(
viewBinding.textViewLegalNotice,
Pattern.compile(privacyPolicyText),
null,
null,
(match, url) -> "https://policies.google.com/privacy"
);
在花了一段时间之后,我发现:
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布局中,如果你想要所有的链接都是可点击的。
在SpannableString上创建一个扩展方法:
private fun SpannableString.setLinkSpan(text: String, url: String) {
val textIndex = this.indexOf(text)
setSpan(
object : ClickableSpan() {
override fun onClick(widget: View) {
Intent(Intent.ACTION_VIEW).apply { data = Uri.parse(url) }.also { startActivity(it) }
}
},
textIndex,
textIndex + text.length,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
使用它使字符串在你的TextView可点击:
myTextView.apply {
movementMethod = LinkMovementMethod.getInstance()
val googleUrl = "http://www.google.com"
val microsoftUrl = "http://www.microsoft.com"
val google = "Google"
val microsoft = "Microsoft"
val message = SpannableString("$google & $microsoft").apply {
setLinkSpan(google, googleUrl)
setLinkSpan(microsoft, microsoftUrl)
}
text = message
}
享受吧!