Last active
August 29, 2015 14:05
-
-
Save XinyueZ/2177736f6fbad80b2622 to your computer and use it in GitHub Desktop.
Hyperlink on TextView, better solved.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Inspired by http://stackoverflow.com/a/20647011/1835650 | |
| //Inspired by http://aichixihongshi.iteye.com/blog/1207503 | |
| SpannableStringBuilder spanBuilder = (SpannableStringBuilder) p.getSpannedText(); //Html.from(.....) | |
| URLSpan[] spans = spanBuilder.getSpans(0, spanBuilder.length(), URLSpan.class); | |
| for (URLSpan s: spans) { | |
| int start = spanBuilder.getSpanStart(s); | |
| int end = spanBuilder.getSpanEnd(s); | |
| spanBuilder.removeSpan(s); | |
| CustomClickableSpan sp = new CustomClickableSpan(s, false); | |
| spanBuilder.setSpan(sp, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); | |
| } | |
| //We don't set linkable, linkclickable in xml otherwise the follow link doesn't work, see. http://stackoverflow.com/a/20647011/1835650 | |
| tv.setMovementMethod(LinkMovementMethod.getInstance()); | |
| tv.setText(spanBuilder); | |
| //Only a demo for customized ClickableSpan, not very necessary. | |
| private class CustomClickableSpan extends ClickableSpan { | |
| private boolean mIsInternal; | |
| private URLSpan mSpan; | |
| public CustomClickableSpan(URLSpan _span, boolean _internal) { | |
| super(); | |
| mSpan = _span; | |
| mIsInternal = _internal; | |
| } | |
| Override | |
| public void onClick(View _v) { | |
| if (mIsInternal) { | |
| } else { | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment