首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

TextView 超链接事件擒获

2012-09-07 
TextView 超链接事件捕获TextView tv (TextView) findViewById(R.id.txtView)ctx thisString htmlLi

TextView 超链接事件捕获

TextView tv = (TextView) findViewById(R.id.txtView);ctx = this;String htmlLinkText = "";htmlLinkText = "<a style="color:red;" href=""+strUrl+"">三孔游</a>";// 文字的样式(style)被覆盖,不能改变……tv.setText(Html.fromHtml(htmlLinkText));tv.setMovementMethod(LinkMovementMethod.getInstance());CharSequence text = tv.getText();if (text instanceof Spannable) {int end = text.length();Spannable sp = (Spannable) tv.getText();URLSpan[] urls = sp.getSpans(0, end, URLSpan.class);SpannableStringBuilder style = new SpannableStringBuilder(text);style.clearSpans();// should clear old spansfor (URLSpan url : urls) {MyURLSpan myURLSpan = new MyURLSpan(url.getURL());style.setSpan(myURLSpan, sp.getSpanStart(url),sp.getSpanEnd(url), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);}tv.setText(style);}

private static class MyURLSpan extends ClickableSpan {private String mUrl;MyURLSpan(String url) {mUrl = url;}@Overridepublic void onClick(View widget) {Toast.makeText(ctx, mUrl, Toast.LENGTH_LONG).show();widget.setBackgroundColor(Color.parseColor("#00000000"));}}


转自 http://hi.baidu.com/%CD%F5%D5%F1%C1%D6wzl/blog/item/c0ef56b7739cfcb7d8335aef.html

热点排行