TextView自定义省略号、部分文字变色、部分文字点击

1、如果文本内容超过最大行数,在两个字符串拼接处添加省略号省略代码:

ViewTreeObserver observer = textView.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

@Override
public void onGlobalLayout() {
ViewTreeObserver obs = textView.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
if (textView.getLineCount() > maxLine) {//判断行数大于多少时改变
int lineEndIndex = textView.getLayout().getLineEnd(maxLine - 1); //设置第maxLine行打省略号
String subStr = str.substring(str.lastIndexOf(strFlag));
Log.i("subStr", subStr);
String text = textView.getText().subSequence(0, lineEndIndex - subStr.length()) + "..." + subStr;
textView.setText(text);
}
}
});
}

2、部分文字变颜色:

String str = "小明回复小红:";
int firstStartIndex = 0;
int firstBendIndex = str.indexOf("回复");
int secondStartIndex = str.indexOf("回复") + "回复".length();
int secondBendIndex = str.indexOf(":");
SpannableStringBuilder style = new SpannableStringBuilder(str);
style.setSpan(new BackgroundColorSpan(Color.BLACK), firstStartIndex, firstBendIndex, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
style.setSpan(new ForegroundColorSpan(Color.BLUE), firstStartIndex, firstBendIndex, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
style.setSpan(new ForegroundColorSpan(Color.RED), secondStartIndex, secondBendIndex, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
text_view.setText(style);

3、部分文字可点击:

String str = "小明回复小红:";
int firstStartIndex = 0;
int firstBendIndex = str.indexOf("回复");
int secondStartIndex = str.indexOf("回复") + "回复".length();
int secondBendIndex = str.indexOf(":");
SpannableString spannableString = new SpannableString(str);
spannableString.setSpan(new MyClickableSpan(new OnTextViewClickListener() {
@Override
public void setTextClick() {
Toast.makeText(MainActivity.this, "小明", Toast.LENGTH_SHORT).show();
}
}), firstStartIndex, firstBendIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);


spannableString.setSpan(new MyClickableSpan(new OnTextViewClickListener() {
@Override
public void setTextClick() {
Toast.makeText(MainActivity.this, "小红", Toast.LENGTH_SHORT).show();
}
}), secondStartIndex, secondBendIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//设置点击文字后的文字背景色为透明
text_view.setHighlightColor(getResources().getColor(android.R.color.transparent));
text_view.setMovementMethod(LinkMovementMethod.getInstance());
text_view.setText(spannableString);
public interface OnTextViewClickListener {
void setTextClick();
}

private class MyClickableSpan extends ClickableSpan {
private OnTextViewClickListener onTextViewClickListener;

public MyClickableSpan(OnTextViewClickListener onTextViewClickListener) {
this.onTextViewClickListener = onTextViewClickListener;
}

@Override
public void updateDrawState(TextPaint ds) {
//super.updateDrawState(ds);
//ds.setColor()设定的是span超链接的文本颜色,而不是点击后的颜色,点击后的背景颜色(HighLightColor)属于TextView的属性,Android4.0以上默认是淡绿色,低版本的是黄色。
ds.setColor(Color.BLUE);
}

@Override
public void onClick(View widget) {
onTextViewClickListener.setTextClick();
}
}

猜你喜欢

转载自blog.csdn.net/huideveloper/article/details/52350275