先定义一个clickableSpan的子类

 1 class MyClickText extends ClickableSpan{
 2         private Context context;
 3 
 4         public MyClickText(Context context) {
 5             this.context = context;
 6         }
 7 
 8         @Override
 9         public void updateDrawState(TextPaint ds) {
10             super.updateDrawState(ds);
11             //设置文本的颜色
12             ds.setColor(Color.RED);
13             //超链接形式的下划线,false 表示不显示下划线,true表示显示下划线
14             ds.setUnderlineText(false);
15         }
16 
17         @Override
18         public void onClick(View widget) {
19             Toast.makeText(context,"发生了点击效果",Toast.LENGTH_SHORT).show();
20         }
21     }

然后是在textView中的使用

1 private TextView clicktext;
2         clicktext = (TextView) findViewById(R.id.clicktext);
3      
4         SpannableString str = new SpannableString("超文本:http://www.baidu.com");
5         str.setSpan(new MyClickText(this),4,str.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
6      //当然这里也可以通过setSpan来设置哪些位置的文本哪些颜色
7         clicktext.setText(str);
8         clicktext.setMovementMethod(LinkMovementMethod.getInstance());//不设置 没有点击事件
9         clicktext.setHighlightColor(Color.TRANSPARENT); //设置点击后的颜色为透明

 

posted on 2016-03-15 18:17  毕哥  阅读(1362)  评论(0编辑  收藏  举报