好贷网好贷款

android TextView中超链接的事件捕捉

发布时间:2016-12-4 14:31:54 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"android TextView中超链接的事件捕捉",主要涉及到android TextView中超链接的事件捕捉方面的内容,对于android TextView中超链接的事件捕捉感兴趣的同学可以参考一下。

Java代码 package com.cm; import java.util.ArrayList; import android.R.color; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.os.Bundle; import android.text.Html; import android.tex Java代码  package com.cm;        import java.util.ArrayList;        import android.R.color;    import android.app.Activity;    import android.content.Context;    import android.graphics.Color;    import android.os.Bundle;    import android.text.Html;    import android.text.Spannable;    import android.text.SpannableStringBuilder;    import android.text.method.LinkMovementMethod;    import android.text.style.ClickableSpan;    import android.text.style.URLSpan;    import android.view.View;    import android.widget.TextView;    import android.widget.Toast;        public class TextHtml extends Activity {        private TextView tv;        private static Context ctx;            @Override        public void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);                tv = new TextView(this);            ctx = this;                String htmlLinkText =  "我是超链接"+ "<a style=\"color:red;\" href='lianjie'>超链接点击事件</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 spans                    //循环把链接发过去                          for (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);            }            setContentView(tv);            }            private static class MyURLSpan extends ClickableSpan {                private String mUrl;                MyURLSpan(String url) {                mUrl = url;            }                @Override            public void onClick(View widget) {                if(mUrl .equal(lianjie)){                    Toast.makeText(ctx, mUrl, Toast.LENGTH_LONG).show();                    widget.setBackgroundColor(Color.parseColor("#00000000"));                    }            }        }    }   

上一篇:NSString
下一篇:C++ AMP: Hello C++ AMP!

相关文章

相关评论