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


当前回答

我并不是要把它打得死死的,但下面是在Linkfy等人的被窝里发生的事情。您将注意到setText()接受CharSequence。Linkify等将字符串转换为span,并添加span。Spannable间接继承CharSequence,就像String一样,所以它与setText()一起工作。使用Spannables,你可以混合和匹配跨度,做各种有趣的事情。这里有一个简单的例子。

val textView = findViewById<TextView>(R.id.myTextView)
val span = SpannableStringBuilder(getString(R.string.linkText))
textView [0, textView .length] = URLSpan("https://myWebiste.com/")
textView.text = span
textView.movementMethod = LinkMovementMethod.getInstance()

需要注意的是,Kotlin语法非常流畅,但它混淆了Spannable.setSpan()调用,这就是魔术发生的地方。

其他回答

用这个:

package com.stackoverflow.java.android;

import android.content.Context;
import android.text.method.LinkMovementMethod;
import android.text.method.MovementMethod;
import android.util.AttributeSet;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

public class HyperlinkTextView extends AppCompatTextView {
    public HyperlinkTextView(Context context) {
        super(context);
    }

    public HyperlinkTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public HyperlinkTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * Set default movement method to {@link LinkMovementMethod}
     * @return Link movement method as the default movement method
     */
    @Override
    protected MovementMethod getDefaultMovementMethod() {
        return LinkMovementMethod.getInstance();
    }
}

现在,简单地使用com.stackoverflow.java.android.HyperlinkTextView而不是TextView在你的布局文件将解决你的问题。

如果你想添加一个类似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标签)对我来说是非常误导的…

这是一个非常单行的Android代码,使电话和URL可从textView中选择,无论字符串是什么,数据是什么。您不需要为此使用任何HTML标记。

TextView textView = (TextView)findViewById(R.id.textView1);
textView.setText("some URL is www.google.com phone 7504567890 another URL lkgndflg.com ");

// Makes the textView's Phone and URL (hyperlink) select and go.
Linkify.addLinks(textView, Linkify.WEB_URLS | Linkify.PHONE_NUMBERS);

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

你只需要这样做:

android:autoLink="web"

插入这一行到一个TextView,可以点击与web的引用。URL被设置为这个TextView的文本。

例子:

 <TextView
    android:id="@+id/textViewWikiURL"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:textStyle="bold"
    android:text="http://www.wikipedia.org/"
    android:autoLink="web" />