SpannableString的简单封装

先看效果


使用方法:


代码很简单相信大家都看得懂

接下来上工具类代码

public class SpannableStringUtil {

private SpannableStringBuilder builder=new SpannableStringBuilder();

private ListinfoList=new ArrayList<>();

public static SpannableStringUtil create() {

return new SpannableStringUtil();

}

public SpannableStringUtil setText(String text) {

builder.append(text);

infoList.add(newStringInfo());

StringInfo lastInfo = getLastInfo();

lastInfo.startIndex=builder.length()- text.length();

lastInfo.endIndex=builder.length();

return this;

}

public SpannableStringUtil setTextColor(intcolor) {

ForegroundColorSpan span =new ForegroundColorSpan(color);

StringInfo stringInfo = getLastInfo();

stringInfo.styleList.add(span);

return this;

}

public SpannableStringUtil setTextColor(Context context, intcolorId) {

ForegroundColorSpan span =new ForegroundColorSpan(ContextCompat.getColor(context,colorId));

StringInfo stringInfo = getLastInfo();

stringInfo.styleList.add(span);

return this;

}

public SpannableStringUtil setBgColor(intcolor) {

BackgroundColorSpan span =new BackgroundColorSpan(color);

StringInfo stringInfo = getLastInfo();

stringInfo.styleList.add(span);

return this;

}

publicSpannableStringUtilsetBgColor(Context context, intcolorId) {

BackgroundColorSpan span =newBackgroundColorSpan(ContextCompat.getColor(context,colorId));

StringInfo stringInfo = getLastInfo();

stringInfo.styleList.add(span);

return this;

}

publicSpannableStringUtilsetRelativeSize(floatsize) {

RelativeSizeSpan span =newRelativeSizeSpan(size);

StringInfo stringInfo = getLastInfo();

stringInfo.styleList.add(span);

return this;

}

publicSpannableStringUtilsetImg(Drawable drawable) {

ImageSpan span =newImageSpan(drawable);

builder.append("0");

infoList.add(newStringInfo());

StringInfo lastInfo = getLastInfo();

lastInfo.startIndex=builder.length()-1;

lastInfo.endIndex=builder.length();

lastInfo.styleList.add(span);

return this;

}

publicSpannableStringUtilsetClick(TextView textView, finalOnClickListener listener) {

textView.setMovementMethod(LinkMovementMethod.getInstance());

textView.setHighlightColor(Color.parseColor("#36969696"));

finalStringInfo lastInfo = getLastInfo();

ClickableSpan span =newClickableSpan() {

@Override

public voidupdateDrawState(TextPaint ds) {}

@Override

public voidonClick(View widget) {

listener.onClick(

builder.subSequence(lastInfo.startIndex,lastInfo.endIndex).toString());

}

};

lastInfo.styleList.add(span);

return this;

}

publicSpannableStringUtilsetStyle(booleanbold, booleanintalic) {

StringInfo stringInfo = getLastInfo();

if(bold && intalic) {

StyleSpan span =newStyleSpan(Typeface.BOLD_ITALIC);

stringInfo.styleList.add(span);

}else if(bold){

StyleSpan span =newStyleSpan(Typeface.BOLD);

stringInfo.styleList.add(span);

}else if(intalic) {

StyleSpan span =newStyleSpan(Typeface.ITALIC);

stringInfo.styleList.add(span);

}

return this;

}

publicSpannableStringBuilderbuild() {

for(inti =0;i

StringInfo stringInfo =infoList.get(i);

for(inti1 =0;i1 < stringInfo.styleList.size();i1++) {

CharacterStyle style = stringInfo.styleList.get(i1);

if(style !=null)

builder.setSpan(style,stringInfo.startIndex,stringInfo.endIndex

,Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

}

}

returnbuilder;

}

privateStringInfogetLastInfo() {

returninfoList.get(infoList.size() -1);

}

public interfaceOnClickListener{

voidonClick(String clickStr);

}

private classStringInfo {

intstartIndex;

intendIndex;

ListstyleList=newArrayList<>();

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,308评论 19 139
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,708评论 0 17
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,349评论 0 33
  • 人没有不懒的。 大清早,尤其是在寒冬,被窝暖暖的,要想打个挺就起床,真不容易。荒鸡叫,由它叫。闹钟响,何妨按一下钮...
    65522dd0f5ad阅读 3,746评论 0 1
  • 77w阅读 1,339评论 0 0