Most efficient way for Dynamic Text Color Change in TextView

后端 未结 3 1401
情话喂你
情话喂你 2020-12-17 02:42

I want to change color of parts of a text several times with a timer.

Simplest way is this:

SpannableStringBuilder ssb = new SpannableStringBuilder(m         


        
3条回答
  •  南方客
    南方客 (楼主)
    2020-12-17 03:42

    execute below code once:

    SpannableStringBuilder ssb = new SpannableStringBuilder(mainText);
    ForegroundColorSpan span = new ForegroundColorSpan(Color.BLUE);
    

    and every time you want to change span do this:

    ssb.clearSpans()
    ssb.setSpan(span, start, end, 0);
    tv.setText(ssb);
    

提交回复
热议问题