实现方案:
我们直接参考实例代码:
private String pattern = "((http|ftp|https)://)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\&%_\./-~-]*)?|(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,4})*(/[a-zA-Z0-9\&%_\./-~-]*)?";Pattern r = Pattern.compile(pattern);Matcher m;mTv.setText(IDentifyUrl(richURL.msg));public SpannableStringBuilderForAllvers IDentifyUrl(CharSequence text) { CharSequence contextText; CharSequence clickText; text = text == null ? "" : text; //以下用于拼接本来存在的spanText SpannableStringBuilderForAllvers span = new SpannableStringBuilderForAllvers(text); ClickableSpan[] clickableSpans = span.getSpans(0,text.length(),ClickableSpan.class); if (clickableSpans.length > 0) { int start = 0; int end = 0; for (int i = 0; i < clickableSpans.length; i++) { start = span.getSpanStart(clickableSpans[0]); end = span.getSpanEnd(clickableSpans[i]); } //可点击文本后面的内容页 contextText = text.subSequence(end,text.length()); //可点击文本 clickText = text.subSequence(start,end); } else { contextText = text; clickText = null; } m = r.matcher(contextText); //匹配成功 while (m.find()) { //得到网址数m.group() if (m.start() < m.end()) { span.setSpan(new linkClickSpan(m.group(),m.group(),mUrlSpanClickListener),m.start(),m.end(),Spanned.SPAN_INCLUSIVE_EXCLUSIVE); } } return span;}private static final String httpS = "https://";private static final String http = "http://";private static final String FTP = "ftp://";public static boolean hasNetUrlhead(String url) { return (!TextUtils.isEmpty(url)) && (url.startsWith(http) || url.startsWith(httpS) || url.startsWith(FTP));}private UrlSpanClickListener mUrlSpanClickListener = new UrlSpanClickListener() { @OverrIDe public voID onClick(VIEw vIEw,String url,String content) { if (TextUtils.isEmpty(url)) { return; } Matcher url_matcher = Patterns.WEB_URL.matcher(url); if (url_matcher.matches()) { String tempurl; if (hasNetUrlhead(url)) { tempurl = url; } else { tempurl = httpS + url; } //通过webvIEw打开相应的url //Bundle bundle = new Bundle(); //bundle.putString(WebCordovaBaseFragment.EXTRA_URL,tempurl); //bundle.putBoolean(WebCordovaBaseFragment.ENABLE_WEB_Title,true); //WebVIEwActivity.presentWeb(UtilitIEs.getApplicationContext(),WebVIEwActivity.class,WebCommonFragment.class,content,bundle); } }};public interface UrlSpanClickListener { voID onClick(VIEw vIEw,String content);}public static class linkClickSpan extends ClickableSpan { private int mcolor = UtilitIEs.getApplicationContext().getResources().getcolor(R.color.yc_color_007AFF_CBN); private String mUrl; private String mContent; UrlSpanClickListener mClickListener; public linkClickSpan(String url,String content,UrlSpanClickListener onClickListener) { super(); mUrl = url; mContent = content; mClickListener = onClickListener; } @OverrIDe public voID updateDrawState(TextPaint ds) { ds.setcolor(mcolor); ds.linkcolor = mcolor; ds.setUnderlineText(true);//设置是否下划线 ds.clearShadowLayer(); } @OverrIDe public voID onClick(VIEw Widget) { if (mClickListener != null) { mClickListener.onClick(Widget,mUrl,mContent); } }}public class SpannableStringBuilderForAllvers extends SpannableStringBuilder{ public SpannableStringBuilderForAllvers() { super(""); } public SpannableStringBuilderForAllvers(CharSequence text) { super(text,text.length()); } public SpannableStringBuilderForAllvers(CharSequence text,int start,int end){ super(text,start,end); } @OverrIDe public SpannableStringBuilder append(CharSequence text) { if (text == null) { return this; } int length = length(); return (SpannableStringBuilderForAllvers)replace(length,length,text,text.length()); } /**该方法在原API里面只支持API21或者以上,这里适应低版本*/ public SpannableStringBuilderForAllvers append(CharSequence text,Object what,int flags) { if (text == null) { return this; } int start = length(); append(text); setSpan(what,length(),flags); return this; }}public class ClickableSpanTextVIEw extends AppCompatTextVIEw { private BackgroundcolorSpan backgroundcolorSpan; private boolean hasspan; public ClickableSpanTextVIEw(Context context) { super(context); init(); } public ClickableSpanTextVIEw(Context context,AttributeSet attrs) { super(context,attrs); init(); } public ClickableSpanTextVIEw(Context context,AttributeSet attrs,int defStyleAttr) { super(context,attrs,defStyleAttr); init(); } private voID init() { setMovementMethod(linkMovementMethod.getInstance()); backgroundcolorSpan = new BackgroundcolorSpan(getContext().getResources().getcolor(R.color.yc_color_4B4B4B_CDG)); } @OverrIDe public boolean ontouchEvent(MotionEvent event) { boolean handled = super.ontouchEvent(event); int action = event.getAction(); if (!(getText() instanceof Spannable)) { return handled; } Spannable spannable = (Spannable) getText(); if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) { int x = (int) event.getX(); int y = (int) event.getY(); x -= getTotalpaddingleft(); y -= getTotalpaddingtop(); x += getScrollX(); y += getScrollY(); Layout layout = getLayout(); int line = layout.getlineForVertical(y); int off = layout.getoffsetForHorizontal(line,x); if (off >= getText().length()) { int off1 = layout.getoffsetForHorizontal(line,x - getTextSize()); if (off == off1) { return handled; } } ClickableSpan[] links = spannable.getSpans(off,off,ClickableSpan.class); if (links.length > 0) { ClickableSpan clickableSpan = links[0]; int start = spannable.getSpanStart(clickableSpan); int end = spannable.getSpanEnd(clickableSpan); if (action == MotionEvent.ACTION_DOWN && !hasspan) { spannable.setSpan(backgroundcolorSpan,end,Spanned.SPAN_INCLUSIVE_INCLUSIVE); hasspan = true; } else if (hasspan) { spannable.removeSpan(backgroundcolorSpan); hasspan = false; } } return links.length != 0; } else { if (hasspan && action != MotionEvent.ACTION_MOVE) { spannable.removeSpan(backgroundcolorSpan); hasspan = false; } } return handled; }}
以上实例代码大家可以测试下,感谢大家的学习和对我们的支持。
总结以上是内存溢出为你收集整理的android TextView中识别多个url并分别点击跳转方法详解全部内容,希望文章能够帮你解决android TextView中识别多个url并分别点击跳转方法详解所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)