一.简介:
1.实现文字变色以及点击,都需要使用到SpannableString,实例化该类只需将你想要处理的字符串当做参数 如:SpannableString ssContent = new SpannableString(content);
2.指定文字的点击事件设置,及文字变色,交给ClickableSpan可同时设置文本颜色及点击事件,先写一个类继承ClickableSpan。可根据自己的需求,给实现类添加相应的字段。
二.demo演示:
class TextViewSpan extends ClickableSpan {
private Context mContext;
private int color;//字体颜色
private int type;//用户类型
............
public TextViewSpan(Context mContext,int color,int type) {
this.mContext = mContext;
this.color = color;
this.type = type;
}
//字体设置颜色
@Override
public void updateDrawState(TextPaint ds) {
if(color != 0){
ds.setColor(color);
}
ds.setUnderlineText(false); // 去掉下划线
}
@Override
public void onClick(View widget) {
//此处做事件处理
}
}
1.创建ClickableSpan的对象,ClickableSpan csContent = new TextViewSpan(mContext,Color.parseColor("#94cde9"),0,type.....);
2.调用SpannableString 对象的 setSpan方法:ssContent.setSpan(csContent,0,content.length(),Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
3.最后textview.setText(ssContent);
//执行如下方法 就可以生效了,可使各部分文本获取焦点
textview.setMovementMethod(LinkMovementMethod.getInstance());
textview.setHighlightColor(mContext.getResources().getColor(android.R.color.transparent));
***以上就完成了对部分文本的 着色及点击处理.如果有多部分内容处理时,将各个部分按照以上方法逐一设置,最后使用textview的 append方法进行拼接。
例如: 求双击回复花一样的年华:666