直接上代码
String textStr = "文字内容"
int end = textStr.length();
Spannable sp = (Spannable) Html.fromHtml(textStr);
URLSpan[] urls = sp.getSpans(0, end, URLSpan.class);
SpannableStringBuilder style = new SpannableStringBuilder(sp);
style.clearSpans();
if (urls != null && urls.length > 0) {
for (URLSpan url : urls) {
MyURLSpan myURLSpan = new MyURLSpan(url.getURL());
style.setSpan(myURLSpan, sp.getSpanStart(url), sp.getSpanEnd(url), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
} else {
style = new SpannableStringBuilder(textStr);
style.clearSpans();
Pattern pattern = Pattern.compile("(https?|codoon)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]");
Matcher matcher = pattern.matcher(textStr);
while (matcher.find()) {
int startAt = matcher.start();
int endAt = matcher.end();
MyURLSpan myURLSpan = new MyURLSpan(textStr.substring(startAt, endAt));
style.setSpan(myURLSpan, startAt, endAt, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
style.replace(startAt,endAt,"链接");
}
}