android實(shí)現(xiàn)風(fēng)格文本

在平時(shí)的開(kāi)發(fā)中經(jīng)常遇到文本中帶有數(shù)字或者字母之類改變文本顏色添加下劃線,或在文本左側(cè)、右側(cè)添加圖片等需求,這里自己寫(xiě)了個(gè)非常實(shí)用的類,定義顏色圖片等(支持多規(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è)置顏色 點(diǎn)擊監(jiān)聽(tīng)的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) {//點(diǎn)擊事件

? ? ? ? ? ? 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);

? ? }

//點(diǎn)擊監(jiān)聽(tīng)-全局

? ? 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]熱愛(ài)工作Work2018[d_aini]");

? diytextview.append("\n[d_doge]熱愛(ài)生活Life2018[d_aini]");

? diytextview.append("\n[d_doge]熱愛(ài)學(xué)習(xí)Learn1314[d_aini]");

? diytextview.append("\n[d_doge]熱愛(ài)學(xué)習(xí)Learn1314[d_aini]");

? diytextview.append("\n[d_doge]熱愛(ài)學(xué)習(xí)Learn1314[d_aini]");

? diytextview.append("\n[d_doge]熱愛(ài)學(xué)習(xí)Learn1314[d_aini]");

? diytextview.append("\n[d_doge]熱愛(ài)學(xué)習(xí)Learn1314[d_aini]");

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容