package com.fanxin.huangfangyi.main.utils; import android.content.Context; import android.content.Intent; import android.os.Handler; import android.text.SpannableStringBuilder; import android.text.Spanned; import android.text.TextPaint; import android.text.method.LinkMovementMethod; import android.text.style.ClickableSpan; import android.view.View; import android.widget.TextView; import com.fanxin.huangfangyi.main.moments.MyWebViewActivity; /** * Created by huangfangyi on 2016/7/10.\ * QQ:84543217 */ public class UrlTextViewUtils { private Context context; public UrlTextViewUtils(Context context) { this.context = context; } public void setUrlTextView(String test_temp, TextView tv_content) { String test = test_temp; if ((test_temp != null) && (test_temp.contains("http://") || test_temp.contains("https://") || test_temp .contains("www."))) { int start = 0; while (test != null && !(test.startsWith("http://") || test.startsWith("https://") || test .startsWith("www."))) { test = test.substring(1); start++; } int end = 0; for (int i = 0; i < test.length(); i++) { char item = test.charAt(i); if (isChinese(item) || item == ' ') { break; } end = i; } String result = (String) test_temp .substring(start, start + end + 1); if (result != null) { SpannableStringBuilder ssb = new SpannableStringBuilder(); ssb.append(test_temp); ssb.setSpan(new ContentURLSpan(result), start, start + end + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); tv_content.setText(ssb); tv_content.setMovementMethod(LinkMovementMethod.getInstance()); } } else { tv_content.setText(test_temp); } } // 根据Unicode编码完美的判断中文汉字和符号 private boolean isChinese(char c) { Character.UnicodeBlock ub = Character.UnicodeBlock.of(c); if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) { return true; } return false; } private class ContentURLSpan extends ClickableSpan { private String url; public ContentURLSpan(String url) { this.url = url; } @Override public void updateDrawState(TextPaint ds) { ds.setUnderlineText(false); // 去掉下划线 } @Override public void onClick(final View widget) { if (widget instanceof TextView) { ((TextView) widget).setHighlightColor(context.getResources() .getColor(android.R.color.darker_gray)); new Handler().postDelayed(new Runnable() { public void run() { ((TextView) widget).setHighlightColor(context .getResources().getColor( android.R.color.transparent)); } }, 1000); } context.startActivity(new Intent(context, MyWebViewActivity.class) .putExtra("url", url)); } } }