1.
定义 SpannableStringBuilder
SpannableStringBuilder builder = new SpannableStringBuilder();
复制代码
2.
调用 SpannableStringUtils中的相应方法
SpannableStringUtils.addColorTextWithClickEvent(R.color.dynamic_detail_recomment_name_color,builder,bean.getReply_name()+": ",new SpannableStringUtils.OnTextClickListener(){
@Override
public void onClick() {
mReplay.onUserClickListener(bean.getReply_uid());
}
});
复制代码
3.
把最后的Builderset进去
tvComment.setText(builder);
复制代码
4.
下面是对应的工具类大家复制即可,其中BaseApplication是应用的Application实例
public class SpannableStringUtils {
//添加指定颜色的文字到spannable中
public static void addColorTextToMsg(int color, SpannableStringBuilder spsBuilder, String content){
SpannableString contentSps = new SpannableString(content);
contentSps.setSpan(new ForegroundColorSpan(color), 0,content.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spsBuilder.append(contentSps);
}
public static void addColorTextWithClickEvent(final int color, SpannableStringBuilder spsBuilder, String content, final OnTextClickListener onTextClickListener){
SpannableString contentSps = new SpannableString(content);
// contentSps.setSpan(new ForegroundColorSpan(color), 0,content.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
contentSps.setSpan(new ClickableSpan() {
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setColor(BaseApplication.getInstance().getResources().getColor(color));
ds.setUnderlineText(false); //设置下划线
}
@Override
public void onClick(View widget) {
onTextClickListener.onClick();
}
}, 0,content.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spsBuilder.append(contentSps);
}
public interface OnTextClickListener{
void onClick();
}
}
复制代码