首先,定义一个可点击的文本类,包含文本内容和点击监听:
public class ClickText {
private String text;
public interface OnClickListener {
void onClick();
}
private OnClickListener onClickListener;
public ClickText(String text) {
this.text = text;
}
public ClickText(String text, OnClickListener onClickListener) {
this.text = text;
this.onClickListener = onClickListener;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public OnClickListener getOnClickListener() {
return onClickListener;
}
public void setOnClickListener(OnClickListener onClickListener) {
this.onClickListener = onClickListener;
}
}
其次,对各需要点击的文本进行处理:
public class MultiText {
public static class Builder {
int defaultColor = Color.BLACK;
int specialColor = Color.BLUE;
boolean showUnderLine = false;
String totalText = "";
List<ClickText> clickTextList = new ArrayList<>(4);
/**
* 设置默认颜色
*/
public Builder setDefaultColor(@ColorInt int color) {
defaultColor = color;
return this;
}
/**
* 设置指定颜色
*/
public Builder setSpecialColor(@ColorInt int color) {
specialColor = color;
return this;
}
/**
* 设置下划线
*/
public Builder setShowUnderLine(boolean isShowUnderLine) {
showUnderLine = isShowUnderLine;
return this;
}
/**
* 设置全部文本
*/
public Builder setTotalText(String text) {
totalText = text;
return this;
}
/**
* 设置点击文本
*/
public Builder addClickText(ClickText... clickTexts) {
for (ClickText clickText : clickTexts) {
if (clickText != null) {
clickTextList.add(clickText);
}
}
return this;
}
public void apply(TextView textView) {
if (TextUtils.isEmpty(totalText) || clickTextList.size() == 0) {
return;
}
totalText = totalText.trim();
SpannableString spannableString = new SpannableString(totalText);
int position;
int index = 0;
for (int i = 0; i < clickTextList.size(); i++) {
ClickText clickText = clickTextList.get(i);
position = totalText.indexOf(clickText.getText(), index);
// 找不到
if (position == -1) {
continue;
}
int length = clickText.getText().length();
if (position != index) {
spannableString.setSpan(new ForegroundColorSpan(defaultColor), index, position, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
spannableString.setSpan(new ForegroundColorSpan(specialColor), position, position + length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
final ClickText.OnClickListener onClickListener = clickText.getOnClickListener();
spannableString.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
if (onClickListener != null) {
onClickListener.onClick();
}
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setUnderlineText(false);
}
}, position, position + length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
if (showUnderLine) {
spannableString.setSpan(new UnderlineSpan(), position, position + length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
index = position + length;
}
if (index < totalText.length()) {
spannableString.setSpan(new ForegroundColorSpan(defaultColor), index, totalText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(Color.TRANSPARENT);
textView.setText(spannableString);
}
}
public static Builder newBuilder() {
return new Builder();
}
}