一.簡介:
1.實現(xiàn)文字變色以及點擊,都需要使用到SpannableString,實例化該類只需將你想要處理的字符串當做參數(shù) 如:SpannableString ssContent = new SpannableString(content);
2.指定文字的點擊事件設(shè)置,及文字變色,交給ClickableSpan可同時設(shè)置文本顏色及點擊事件,先寫一個類繼承ClickableSpan。可根據(jù)自己的需求,給實現(xiàn)類添加相應(yīng)的字段。
二.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;
}
//字體設(shè)置顏色
@Override
public void updateDrawState(TextPaint ds) {
if(color != 0){
ds.setColor(color);
}
ds.setUnderlineText(false); // 去掉下劃線
}
@Override
public void onClick(View widget) {
//此處做事件處理
}
}
1.創(chuàng)建ClickableSpan的對象,ClickableSpan csContent = new TextViewSpan(mContext,Color.parseColor("#94cde9"),0,type.....);
2.調(diào)用SpannableString 對象的 setSpan方法:ssContent.setSpan(csContent,0,content.length(),Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
3.最后textview.setText(ssContent);
//執(zhí)行如下方法 就可以生效了,可使各部分文本獲取焦點
textview.setMovementMethod(LinkMovementMethod.getInstance());
textview.setHighlightColor(mContext.getResources().getColor(android.R.color.transparent));
***以上就完成了對部分文本的 著色及點擊處理.如果有多部分內(nèi)容處理時,將各個部分按照以上方法逐一設(shè)置,最后使用textview的 append方法進行拼接。
例如: 求雙擊回復花一樣的年華:666