在平時的開發(fā)中經(jīng)常遇到文本中帶有數(shù)字或者字母之類改變文本顏色添加下劃線,或在文本左側(cè)、右側(cè)添加圖片等需求棒掠,這里自己寫了個非常實用的類,定義顏色圖片等(支持多規(guī)則)
首先看效果圖:
自定義DiyStyleTextView繼承自AppCompatTextView
文本是否顯示下劃線
public DiyStyleTextViewsetUnderlineText(boolean underlineText) {
this.underlineText = underlineText;
return this;
}
添加自定義顏色規(guī)則屁商,滿足該正則表達(dá)式的文本自定義顏色烟很,colorRegex正則表達(dá)式,color自定義的顏色
public DiyStyleTextViewaddColorRegex(String colorRegex, int color) {
setMovementMethod(LinkMovementMethod.getInstance());
? ? colorRegexList.add(colorRegex);
? ? colorList.add(color);
return this;
}
添加自定義圖片規(guī)則
public DiyStyleTextViewaddImageRegex(String imageRegex, Bitmap bitmap) {
setMovementMethod(LinkMovementMethod.getInstance());
? ? imageRegexList.add(imageRegex);
? ? bitmapList.add(bitmap);
return this;
}
最后貼上代碼:
public class DiyStyleTextViewextends AppCompatTextView {
private boolean underlineText =false;
? ? private ListcolorRegexList =new ArrayList<>();
? ? private ListcolorList =new ArrayList<>();
? ? private ListimageRegexList =new ArrayList<>();
? ? private ListbitmapList =new ArrayList<>();
? ? public DiyStyleTextView(Context context) {
super(context);
? ? }
public DiyStyleTextView(Context context, AttributeSet attrs) {
super(context, attrs);
? ? }
//是否顯示下劃線-全局
? ? public DiyStyleTextViewsetUnderlineText(boolean underlineText) {
this.underlineText = underlineText;
return this;
? ? }
//添加自定義顏色規(guī)則
? ? public DiyStyleTextViewaddColorRegex(String colorRegex, int color) {
setMovementMethod(LinkMovementMethod.getInstance());
? ? ? ? colorRegexList.add(colorRegex);
? ? ? ? colorList.add(color);
return this;
? ? }
//添加自定義圖片規(guī)則
? ? public DiyStyleTextViewaddImageRegex(String imageRegex, Bitmap bitmap) {
setMovementMethod(LinkMovementMethod.getInstance());
? ? ? ? imageRegexList.add(imageRegex);
? ? ? ? bitmapList.add(bitmap);
return this;
? ? }
//覆蓋父類,setText()生效
? ? @Override
? ? public void setText(CharSequence text, BufferType type) {
super.setText(setTextStyle(text, false), type);
? ? }
//覆蓋父類,append()生效
? ? @Override
? ? public void append(CharSequence text, int start, int end) {
super.append(setTextStyle(text, false), start, end);
? ? }
public void setDiyTextColor(CharSequence text, String regularExpression, int color, DiyTextClick mDiyTextClick) {
addColorRegex(regularExpression, color).setDiyTextClickListenner(mDiyTextClick).setTextStyle(text, true);
? ? }
public void setDiyTextColor(CharSequence text, String regularExpression, int color) {
setDiyTextColor(text, regularExpression, color, null);
? ? }
public void setDiyTextImage(CharSequence text, String regularExpression, Bitmap bitmap, DiyTextClick mDiyTextClick) {
addImageRegex(regularExpression, bitmap).setDiyTextClickListenner(mDiyTextClick).setTextStyle(text, true);
? ? }
public void setDiyTextImage(CharSequence text, String regularExpression, Bitmap bitmap) {
setDiyTextImage(text, regularExpression, bitmap, null);
? ? }
private ListindexArr =new ArrayList<>();
? ? private ListstrArr =new ArrayList<>();
? ? public CharSequencesetTextStyle(CharSequence text, boolean flag) {
if (TextUtils.isEmpty(text)) {
if (flag)super.setText(text);
? ? ? ? ? ? return text;
? ? ? ? }
SpannableStringBuilder styledText =new SpannableStringBuilder(text);
? ? ? ? if (colorRegexList !=null)
for (int j =0; j
String colorRegex =colorRegexList.get(j);
? ? ? ? ? ? ? ? int color =colorList.get(j);
? ? ? ? ? ? ? ? indexArr.clear();
? ? ? ? ? ? ? ? strArr.clear();
? ? ? ? ? ? ? ? //正則獲取符合的字符串
? ? ? ? ? ? ? ? Pattern p = Pattern.compile(colorRegex);
? ? ? ? ? ? ? ? Matcher m = p.matcher(text);
? ? ? ? ? ? ? ? while (m.find()) {
strArr.add(m.group());
? ? ? ? ? ? ? ? ? ? indexArr.add(m.start());
? ? ? ? ? ? ? ? }
for (int i =0; i
int index =indexArr.get(i);
? ? ? ? ? ? ? ? ? ? String clickText =strArr.get(i);
? ? ? ? ? ? ? ? ? ? //替換文本 自定義風(fēng)格
? ? ? ? ? ? ? ? ? ? styledText.setSpan(
diyTextClickListenner ==null ?
new TextViewCharacterStyle(color) :
new TextViewClickSpan(clickText, color),
? ? ? ? ? ? ? ? ? ? ? ? ? ? index,
? ? ? ? ? ? ? ? ? ? ? ? ? ? index + clickText.length(),
? ? ? ? ? ? ? ? ? ? ? ? ? ? Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
? ? ? ? ? ? ? ? }
}
if (imageRegexList !=null)
for (int j =0; j
String imageRegex =imageRegexList.get(j);
? ? ? ? ? ? ? ? Bitmap bitmap =bitmapList.get(j);
? ? ? ? ? ? ? ? indexArr.clear();
? ? ? ? ? ? ? ? strArr.clear();
? ? ? ? ? ? ? ? Pattern p = Pattern.compile(imageRegex);
? ? ? ? ? ? ? ? Matcher m = p.matcher(text);
? ? ? ? ? ? ? ? while (m.find()) {
strArr.add(m.group());
? ? ? ? ? ? ? ? ? ? indexArr.add(m.start());
? ? ? ? ? ? ? ? }
for (int i =0; i
int index =indexArr.get(i);
? ? ? ? ? ? ? ? ? ? String clickText =strArr.get(i);
? ? ? ? ? ? ? ? ? ? styledText.setSpan(
new ImageSpan(bitmap),
? ? ? ? ? ? ? ? ? ? ? ? ? ? index,
? ? ? ? ? ? ? ? ? ? ? ? ? ? index + clickText.length(),
? ? ? ? ? ? ? ? ? ? ? ? ? ? Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
? ? ? ? ? ? ? ? ? ? styledText.setSpan(
diyTextClickListenner ==null ?
null :
new TextViewClickSpan(clickText, 0),
? ? ? ? ? ? ? ? ? ? ? ? ? ? index,
? ? ? ? ? ? ? ? ? ? ? ? ? ? index + clickText.length(),
? ? ? ? ? ? ? ? ? ? ? ? ? ? Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
? ? ? ? ? ? ? ? }
}
if (flag)super.setText(styledText);
? ? ? ? return styledText;
? ? }
//設(shè)置顏色 點擊監(jiān)聽的Span
? ? private class TextViewClickSpanextends ClickableSpan {
private StringclickText;
? ? ? ? private int color;
? ? ? ? TextViewClickSpan(String clickText) {
this.clickText = clickText;
? ? ? ? }
TextViewClickSpan(String clickText, int color) {
this.clickText = clickText;
? ? ? ? ? ? this.color = color;
? ? ? ? }
@Override
? ? ? ? public void updateDrawState(TextPaint ds) {
if (color !=0) ds.setColor(color);
? ? ? ? ? ? ds.setUnderlineText(underlineText); //下劃線
? ? ? ? }
@Override
? ? ? ? public void onClick(View widget) {//點擊事件
? ? ? ? ? ? if (diyTextClickListenner !=null)
diyTextClickListenner.diyTextClick(clickText);
? ? ? ? }
}
//只有設(shè)置顏色Span
? ? private class TextViewCharacterStyleextends CharacterStyle {
private int color;
? ? ? ? TextViewCharacterStyle(int color) {
this.color = color;
? ? ? ? }
@Override
? ? ? ? public void updateDrawState(TextPaint ds) {
if (color !=0) ds.setColor(color);
? ? ? ? ? ? ds.setUnderlineText(underlineText); //下劃線
? ? ? ? }
}
private DiyTextClickdiyTextClickListenner;
? ? public interface DiyTextClick {
void diyTextClick(String s);
? ? }
//點擊監(jiān)聽-全局
? ? public DiyStyleTextViewsetDiyTextClickListenner(DiyTextClick mDiyTextClick) {
this.diyTextClickListenner = mDiyTextClick;
? ? ? ? setClickable(true);
return this;
? ? }
}
使用方法:
diytextview.addColorRegex("[\\d]+", 0xff2CA298)
? ? ? ? ? ? ? ? .addColorRegex("[a-zA-Z]+", 0xffff6666)
? ? ? ? ? ? ? ? .addImageRegex("\\[d_aini\\]", BitmapFactory.decodeResource(getResources(), R.drawable.d_aini))
? ? ? ? ? ? ? ? .addImageRegex("\\[d_doge\\]", BitmapFactory.decodeResource(getResources(), R.drawable.d_doge));
? diytextview.setText("[d_doge]熱愛工作Work2018[d_aini]");
? diytextview.append("\n[d_doge]熱愛生活Life2018[d_aini]");
? diytextview.append("\n[d_doge]熱愛學(xué)習(xí)Learn1314[d_aini]");
? diytextview.append("\n[d_doge]熱愛學(xué)習(xí)Learn1314[d_aini]");
? diytextview.append("\n[d_doge]熱愛學(xué)習(xí)Learn1314[d_aini]");
? diytextview.append("\n[d_doge]熱愛學(xué)習(xí)Learn1314[d_aini]");
? diytextview.append("\n[d_doge]熱愛學(xué)習(xí)Learn1314[d_aini]");