如何保持可突出显示Android的可点击文字?

时间:2015-07-06 18:59:48

标签: android string colors

我已将字符串中的特定单词设置为可单击,虽然功能正常,但文本始终会突出显示蓝色,直到您将可单击文本外部压到不可单击的文本上。另请注意,膨胀视图或按任何其他按钮也会使文本突出显示。您似乎必须单击不可单击的文本区域才能删除突出显示的可点击文本。单击我可点击的选定单词后,如何防止文本突出显示?这是我的设置

SpannableString ss = new SpannableString(Html.fromHtml(getActivity().getApplicationContext().getResources().getString(R.string.my_string)));

        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Logger.i(TAG, "clicked on clickable words");
            }
        };

        // makes the words clickable
        ss.setSpan(clickableSpan, 10, 23, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        // makes the clickable words remain red link instead of blue
        ss.setSpan(new ForegroundColorSpan(getActivity().getApplicationContext().getResources().getColor(R.color.red)), 30, 43, 0);
        tv.setText(ss);
        tv.setMovementMethod(LinkMovementMethod.getInstance()); 

任何人对此都有任何想法?提前谢谢!

2 个答案:

答案 0 :(得分:2)

我找到答案,tv.setHighlightColor(Color.TRANSPARENT)有效。与重写ClickableSpan并尝试执行类似

之类的操作相比,这是最简单的解决方案
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;
    }
}

希望这个答案可以为某些人节省很多工作!

答案 1 :(得分:0)

尝试使用以下代码从Spannable字符串中删除所有颜色范围:

ForegroundColorSpan[] colorSpans =  ss.getSpans(0, ss.length(), ForegroundColorSpan.class);
    for(ForegroundColorSpan colorSpan : colorSpans){
        ss.removeSpan(colorSpan);
    }