带有可点击链接的 Android TextView:如何捕获点击? [英] Android TextView with Clickable Links: how to capture clicks?

查看:33
本文介绍了带有可点击链接的 Android TextView:如何捕获点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个呈现基本 HTML 的 TextView,包含 2 个以上的链接.我需要捕获链接上的点击并打开链接——在我自己的内部 WebView 中(而不是在默认浏览器中.)

I have a TextView which is rendering basic HTML, containing 2+ links. I need to capture clicks on the links and open the links -- in my own internal WebView (not in the default browser.)

处理链接渲染最常见的方法似乎是这样的:

The most common method to handle link rendering seems to be like this:

String str_links = "<a href='http://google.com'>Google</a><br /><a href='http://facebook.com'>Facebook</a>";
text_view.setLinksClickable(true);
text_view.setMovementMethod(LinkMovementMethod.getInstance());
text_view.setText( Html.fromHtml( str_links ) );

但是,这会导致链接在默认的内部网络浏览器中打开(显示使用...完成操作"对话框).

However, this causes the links to open in the default internal web browser (showing the "Complete Action Using..." dialog).

我尝试实现一个 onClickListener,当点击链接时它会正确触发,但我不知道如何确定点击了哪个链接...

I tried implementing a onClickListener, which properly gets triggered when the link is clicked, but I don't know how to determine WHICH link was clicked...

text_view.setOnClickListener(new OnClickListener(){

    public void onClick(View v) {
        // what now...?
    }

});

或者,我尝试创建自定义 LinkMovementMethod 类并实现 onTouchEvent...

Alternatively, I tried creating a custom LinkMovementMethod class and implementing onTouchEvent...

public boolean onTouchEvent(TextView widget, Spannable text, MotionEvent event) {
    String url = text.toString();
    // this doesn't work because the text is not necessarily a URL, or even a single link... 
    // eg, I don't know how to extract the clicked link from the greater paragraph of text
    return false;
}

想法?

我想出了一个解决方案 解析 HTML 字符串中的链接并使其可点击,然后让您响应 URL.

I came up with a solution which parses the links out of a HTML string and makes them clickable, and then lets you respond to the URL.

推荐答案

基于 另一个答案,这是一个函数 setTextViewHTML() ,它解析 HTML 字符串中的链接并使它们可点击,然后让您响应 URL.

Based upon another answer, here's a function setTextViewHTML() which parses the links out of a HTML string and makes them clickable, and then lets you respond to the URL.

protected void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span)
{
    int start = strBuilder.getSpanStart(span);
    int end = strBuilder.getSpanEnd(span);
    int flags = strBuilder.getSpanFlags(span);
    ClickableSpan clickable = new ClickableSpan() {
        public void onClick(View view) {
            // Do something with span.getURL() to handle the link click...
        }
    };
    strBuilder.setSpan(clickable, start, end, flags);
    strBuilder.removeSpan(span);
}

protected void setTextViewHTML(TextView text, String html)
{
    CharSequence sequence = Html.fromHtml(html);
    SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
    URLSpan[] urls = strBuilder.getSpans(0, sequence.length(), URLSpan.class);   
    for(URLSpan span : urls) {
        makeLinkClickable(strBuilder, span);
    }
    text.setText(strBuilder);
    text.setMovementMethod(LinkMovementMethod.getInstance());       
}

这篇关于带有可点击链接的 Android TextView:如何捕获点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆