Change the text color of a single ClickableSpan when pressed without affecting other ClickableSpans in the same TextView

后端 未结 8 1328
小鲜肉
小鲜肉 2020-11-29 21:19

I have a TextView with multiple ClickableSpans in it. When a ClickableSpan is pressed, I want it to change the color of its text.

I have tried setting a color state

8条回答
  •  情书的邮戳
    2020-11-29 22:00

    Much simpler solution, IMO:

    final int colorForThisClickableSpan = Color.RED; //Set your own conditional logic here.
    
    final ClickableSpan link = new ClickableSpan() {
        @Override
        public void onClick(final View view) {
            //Do something here!
        }
    
        @Override
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setColor(colorForThisClickableSpan);
        }
    };
    

提交回复
热议问题