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

后端 未结 8 1306
小鲜肉
小鲜肉 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:22

    legr3c's answer helped me a lot. And I'd like to add a few remarks.

    Remark #1.

    TextView myTextView = (TextView) findViewById(R.id.my_textview);
    myTextView.setMovementMethod(new LinkTouchMovementMethod());
    myTextView.setHighlightColor(getResources().getColor(android.R.color.transparent));
    SpannableString mySpannable = new SpannableString(text);
    mySpannable.setSpan(new TouchableSpan(), 0, 7, 0);
    mySpannable.setSpan(new TouchableSpan(), 15, 18, 0);
    myTextView.setText(mySpannable, BufferType.SPANNABLE);
    

    I applied LinkTouchMovementMethod to a TextView with two spans. The spans were highlighted with blue when clicked them. myTextView.setHighlightColor(getResources().getColor(android.R.color.transparent)); fixed the bug.

    Remark #2.

    Don't forget to get colors from resources when passing normalTextColor, pressedTextColor, and pressedBackgroundColor.

    Should pass resolved color instead of resource id here

提交回复
热议问题