Android:单击时更改ClickableSpan的背景颜色

时间:2021-09-26 09:33:59

I have a SpannableString with a ClickableSpan as follows

我有一个带有ClickableSpan的SpannableString,如下所示

for (int i = 0; i < items.size(); i++) {
            final SpannableString span = new SpannableString(items.get(i));
            final int index=i;

            span.setSpan(new ClickableSpan() {

                @Override
                public void onClick(View widget) {

                }

                @Override
                public void updateDrawState(TextPaint ds) {
                    ds.setColor(Color.LTGRAY);
                    ds.setUnderlineText(false);

                }
            }, 0, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

            builder.append(span);
        }

//my text view
txt.setText(builder);
txt.setMovementMethod(LinkMovementMethod.getInstance());

what I want to do is to change the foreground color of the span when clicked.

我想要做的是在点击时更改跨度的前景色。

how can I do this ?

我怎样才能做到这一点 ?

3 个解决方案

#1


8  

I extended the clickableSpan class and passed it a flag that lets me know that I should highlight it.

我扩展了clickableSpan类并向它传递了一个标志,让我知道我应该突出显示它。

SpannableStringBuilder tag;
.... tag.setSpan(new WordSpan(i, tokens[i], wordtohighlitedID) { 

.....

.....

import android.graphics.Color;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.view.View;

public class WordSpan extends ClickableSpan 
{

    private int id;
    private TextPaint textpaint;
    public boolean shouldHilightWord = false;
    public WordSpan(int anID, String txt, int selected) {
        id =anID;
        // if the word selected is the same as the ID set the highlight flag
        if(selected == id)  {
            shouldHilightWord = true;

        }


    }

    @Override
    public void updateDrawState(TextPaint ds) {
        textpaint = ds;
        ds.setColor(ds.linkColor);
        if(shouldHilightWord){
            textpaint.bgColor = Color.GRAY;         
            textpaint.setARGB(255, 255, 255, 255);

        }
        //Remove default underline associated with spans
        ds.setUnderlineText(false);

    }

    public void changeSpanBgColor(View widget){
        shouldHilightWord = true;
        updateDrawState(textpaint);
        widget.invalidate();


    }
    @Override
    public void onClick(View widget) {

        // TODO Auto-generated method stub

    }


    /**
     * This function sets the span to record the word number, as the span ID
     * @param spanID
     */
    public void setSpanTextID(int spanID){
        id = spanID;
    }

    /**
     * Return the wordId of this span
     * @return id
     */
    public int getSpanTextID(){
        return id;
    }
}

#2


10  

If you're looking to get rid of the green highlight on selection, this is what you want to know:

如果你想要摆脱选择的绿色亮点,这就是你想知道的:

Apparently, overriding public void updateDrawState(TextPaint ds) in your custom class would not affect the highlight color. It is only used for setting the underline color (or hiding/showing it).

显然,在自定义类中覆盖public void updateDrawState(TextPaint ds)不会影响突出显示颜色。它仅用于设置下划线颜色(或隐藏/显示它)。

All you need to do is: textView.setHighlightColor(Color.TRANSPARENT); where textView is what contains the ClickableSpan.

您需要做的就是:textView.setHighlightColor(Color.TRANSPARENT);其中textView是包含ClickableSpan的内容。

Hope it works for all of you. Feel free to ask any related question.

希望它适用于所有人。随意提出任何相关问题。

#3


-1  

Use this:

用这个:

view.setSelector(new ColorDrawable(Color.BLUE));

#1


8  

I extended the clickableSpan class and passed it a flag that lets me know that I should highlight it.

我扩展了clickableSpan类并向它传递了一个标志,让我知道我应该突出显示它。

SpannableStringBuilder tag;
.... tag.setSpan(new WordSpan(i, tokens[i], wordtohighlitedID) { 

.....

.....

import android.graphics.Color;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.view.View;

public class WordSpan extends ClickableSpan 
{

    private int id;
    private TextPaint textpaint;
    public boolean shouldHilightWord = false;
    public WordSpan(int anID, String txt, int selected) {
        id =anID;
        // if the word selected is the same as the ID set the highlight flag
        if(selected == id)  {
            shouldHilightWord = true;

        }


    }

    @Override
    public void updateDrawState(TextPaint ds) {
        textpaint = ds;
        ds.setColor(ds.linkColor);
        if(shouldHilightWord){
            textpaint.bgColor = Color.GRAY;         
            textpaint.setARGB(255, 255, 255, 255);

        }
        //Remove default underline associated with spans
        ds.setUnderlineText(false);

    }

    public void changeSpanBgColor(View widget){
        shouldHilightWord = true;
        updateDrawState(textpaint);
        widget.invalidate();


    }
    @Override
    public void onClick(View widget) {

        // TODO Auto-generated method stub

    }


    /**
     * This function sets the span to record the word number, as the span ID
     * @param spanID
     */
    public void setSpanTextID(int spanID){
        id = spanID;
    }

    /**
     * Return the wordId of this span
     * @return id
     */
    public int getSpanTextID(){
        return id;
    }
}

#2


10  

If you're looking to get rid of the green highlight on selection, this is what you want to know:

如果你想要摆脱选择的绿色亮点,这就是你想知道的:

Apparently, overriding public void updateDrawState(TextPaint ds) in your custom class would not affect the highlight color. It is only used for setting the underline color (or hiding/showing it).

显然,在自定义类中覆盖public void updateDrawState(TextPaint ds)不会影响突出显示颜色。它仅用于设置下划线颜色(或隐藏/显示它)。

All you need to do is: textView.setHighlightColor(Color.TRANSPARENT); where textView is what contains the ClickableSpan.

您需要做的就是:textView.setHighlightColor(Color.TRANSPARENT);其中textView是包含ClickableSpan的内容。

Hope it works for all of you. Feel free to ask any related question.

希望它适用于所有人。随意提出任何相关问题。

#3


-1  

Use this:

用这个:

view.setSelector(new ColorDrawable(Color.BLUE));