我有以下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"工作,如果你有完整的链接在你的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布局中,如果你想要所有的链接都是可点击的。

其他回答

对于那些从XML内容读取字符串和动态赋值有问题的人。

如果您正在使用来自strings.xml资源的文本,那么HTML标记似乎会被剥离。

所以你必须使用<![CDATA[**your string with click links**]]>在strings.xml文件中使用HTML . fromhtml (string)将其转换为HTML。

用这个:

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在你的布局文件将解决你的问题。

由于数据绑定是出来的,我想分享我的解决方案数据绑定TextViews支持HTML标签与可点击的链接。

为了避免检索每个textview并使用From.html为他们提供html支持,我们扩展了textview并将逻辑放在setText()中

public class HtmlTextView extends TextView {

    public HtmlTextView(Context context) {
        super(context);
    }

    public HtmlTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public HtmlTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        super.setText(Html.fromHtml(text.toString()), type);
        this.setMovementMethod(LinkMovementMethod.getInstance());
    }
}

我已经做了一个主旨,也显示了使用这个实体和视图的例子。

这就是我如何解决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>"));
}

我希望这对你有帮助;

String value = "<html>Visit my blog <a href=\"http://www.maxartists.com\">mysite</a> View <a href=\"sherif-activity://myactivity?author=sherif&nick=king\">myactivity</a> callback</html>";
TextView text = (TextView) findViewById(R.id.text);

text.setText(Html.fromHtml(value));
text.setMovementMethod(LinkMovementMethod.getInstance());