您的位置:首页 > Web前端 > CSS

类似QQ空间中回复列表的样式 "小明 回复 小红"

2016-04-25 10:29 435 查看
类似QQ空间中回复列表的样式 "小明 回复 小红" ,单独textview控件完成。

public class CommentTextView extends TextView {

public CommentTextView<(Context context) {
super(context);
}

public CommentTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

public CommentTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
public void setText(CharSequence text, BufferType type) {
if (!TextUtils.isEmpty(text)) {
super.setText(replace(text.toString()), type);
} else {
super.setText(text, type);
}
}
// 参数传进来
private User namea, nameb;

public void setComment(User namea, User nameb, Context context,
CommentList mcomment) {
this.namea = namea;
this.nameb = nameb;
}

private Pattern buildPattern() {
return Pattern.compile("\\\\ue[a-z0-9]{3}", Pattern.CASE_INSENSITIVE);
}

private CharSequence replace(String text) {
try {
SpannableString spannableString = new SpannableString(text);
int start = 0;
Pattern pattern = buildPattern();
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
String faceText = matcher.group();
String key = faceText.substring(1);
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeResource(
getContext().getResources(),
getContext().getResources().getIdentifier(key,
"drawable", getContext().getPackageName()),
options);
ImageSpan imageSpan = new ImageSpan(getContext(), bitmap);
int startIndex = text.indexOf(faceText, start);
int endIndex = startIndex + faceText.length();
if (startIndex >= 0)
spannableString.setSpan(imageSpan, startIndex, endIndex,
Spannable.SPAN_USER);
start = (endIndex - 1);
}
// 评论者的姓名点击事件
spannableString.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {

}

}, 0, namea.getNickname().length(), Spanned.SPAN_USER);
// 被评论者的姓名点击事件
spannableString.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {

}
}, namea.getNickname().length() + 4, namea.getNickname().length() + 4 + nameb.getNickname().length() - 1,
Spanned.SPAN_USER);
//上面+4是因为实际显示的内容为     "小明(文字变色)(空格)回复(空格)小红(文字变色)"
return spannableString;
} catch (Exception e) {
return text;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息