How to make links in a TextView clickable?

后端 未结 30 3343

I have the following TextView defined:



        
30条回答
  •  遥遥无期
    2020-11-21 23:38

    The accepted answer is correct, BUT it will mean that phone numbers, maps, email addresses, and regular links e.g. http://google.com without href tags will NO LONGER be clickable since you can't have autolink in the xml.

    The only complete solution to have EVERYTHING clickable that I have found is the following:

    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());
    

    And the TextView should NOT have android:autolink. There's no need for android:linksClickable="true" either; it's true by default.

提交回复
热议问题