Android的TextView的可点击的链接:如何捕捉点击?链接、Android、TextView

2023-09-12 04:07:35 作者:迟暮

我有一个TextView是呈现基本的HTML,含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 ) );

不过,这将导致链接在默认的内部Web浏览器打开(显示完成操作使用...对话框)。

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

想法?

我想出了a解决方案其解析环节出了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.

推荐答案

基于another回答,这里有一个功能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);       
}