我有以下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。


当前回答

接受的答案是正确的,但这意味着电话号码、地图、电子邮件地址和常规链接(例如,没有href标记的http://google.com)将不再是可点击的,因为您不能在XML内容中具有自动链接。

我所发现的唯一完整的解决方案是:

Spanned text = Html.fromHtml(myString);
URLSpan[] currentSpans = text.getSpans(0, text.length(), URLSpan.class);
SpannableString buffer = new SpannableString(text);
Linkify.addLinks(buffer, Linkify.ALL);
for (URLSpan span : currentSpans) {
    int end = text.getSpanEnd(span);
    int start = text.getSpanStart(span);
    buffer.setSpan(span, start, end, 0);
}
textView.setText(buffer);
textView.setMovementMethod(LinkMovementMethod.getInstance());

TextView不应该有android:autolink。没有必要android:linksClickable="true";默认情况下是正确的。

其他回答

这就是我如何解决TextView中可点击和可见的链接(通过代码)

private void setAsLink(TextView view, String url){
    Pattern pattern = Pattern.compile(url);
    Linkify.addLinks(view, pattern, "http://");
    view.setText(Html.fromHtml("<a href='http://" + url + "'>http://" + url + "</a>"));
}

你可以简单地添加链接到你的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 &amp; 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"
);

如果你想添加一个类似html的链接,你所需要做的就是:

添加一个类似html的资源字符串: <字符串名称= "链接" > < a href = " https://www.google.pl/ " >谷歌< / > < /字符串> 将你的视图添加到布局中,完全没有链接特定的配置: < TextView android: id =“@ + id /链接” android:文本= " @string /链接" / > ' 添加适当的MovementMethod编程到你的TextView: mLink = (TextView) findViewById(R.id.link); if (mLink != null) { mLink.setMovementMethod (LinkMovementMethod.getInstance ()); }

就是这样!是的,像“autoLink”和“linksClickable”这样的选项只在显式链接上工作(没有包装成HTML标签)对我来说是非常误导的…

理查德,下次,你应该在布局XML的TextView下添加这段代码。

android:autoLink="all"

应该是这样的。

<TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="@string/txtCredits"
    android:id="@+id/infoTxtCredits"
    android:autoLink="all"
    android:linksClickable="true">
</TextView>

您不需要使用此代码(t2.setMovementMethod(LinkMovementMethod.getInstance());)来使链接可单击。

另外,这里有一个事实:只要你设置了autoLink和linksClickable,不要忘记在String.xml文件中添加这个,这样可点击的链接就可以工作了。

<string name="txtCredits"><a href="http://www.google.com">Google</a></string>

在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
    }

享受吧!