在Android Textview中为不同的超链接使用不同的颜色

问题描述 投票:0回答:1

我知道我可以通过设置更改textview级别的超链接颜色:

yourText.setLinkTextColor(Color.RED);

在Textview中,我的应用程序中可以有多个超链接。我想为每个超链接文本定义不同的颜色。不幸的是,使用<font color ...>does不起作用。 有任何想法吗? 谢谢!

android colors hyperlink textview
1个回答
0
投票
 String myString = "I accept the Terms of Use, Privacy Policy and I agree";
    SpannableString ss = new SpannableString(myString);
    ClickableSpan clickableSpan = new TouchableSpan(normalColor, touchColor, pressedColor) {
        @Override
        public void onClick(View widget) {
            //perform your action to open webview or whatever
        }
    };
    ClickableSpan clickableSpan2 = new TouchableSpan(normalColor, touchColor, pressedColor) {
        @Override
        public void onClick(View widget) {
            //perform your action to open webview or whatever
        }
    };
    ss.setSpan(clickableSpan, 13, 25, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ss.setSpan(clickableSpan2, 26, 41, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    textView.text = ss;
    textView.movementMethod =new LinkTouchMovementMethod();

说明

 ss.setSpan(clickableSpan, 13, 25, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

这将使

使用条款

点击。同样,第二个跨度将会产生

隐私政策

可点击我的示例字符串。您可以根据需要使用normal colortouchColorpressedColor

touchable span.Java

public abstract class TouchableSpan extends ClickableSpan {
private boolean mIsPressed;
private int mPressedBackgroundColor;
private int mNormalTextColor;
private int mPressedTextColor;

public TouchableSpan(int normalTextColor, int pressedTextColor, int pressedBackgroundColor) {
    mNormalTextColor = normalTextColor;
    mPressedTextColor = pressedTextColor;
    mPressedBackgroundColor = 0xffeeeeee;
}

public void setPressed(boolean isSelected) {
    mIsPressed = isSelected;
}

@Override
public void updateDrawState(TextPaint ds) {
    super.updateDrawState(ds);
    ds.setColor(mIsPressed ? mPressedTextColor : mNormalTextColor);
    ds.bgColor = mIsPressed ? mPressedBackgroundColor : Color.TRANSPARENT;
    ds.setUnderlineText(false);
}
}

link touch movement method.Java

public class LinkTouchMovementMethod extends LinkMovementMethod {
private TouchableSpan mPressedSpan;

@Override
public boolean onTouchEvent(TextView textView, Spannable spannable, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        mPressedSpan = getPressedSpan(textView, spannable, event);
        if (mPressedSpan != null) {
            mPressedSpan.setPressed(true);
            Selection.setSelection(spannable, spannable.getSpanStart(mPressedSpan),
                    spannable.getSpanEnd(mPressedSpan));
        }
    } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
        TouchableSpan touchedSpan = getPressedSpan(textView, spannable, event);
        if (mPressedSpan != null && touchedSpan != mPressedSpan) {
            mPressedSpan.setPressed(false);
            mPressedSpan = null;
            Selection.removeSelection(spannable);
        }
    } else {
        if (mPressedSpan != null) {
            mPressedSpan.setPressed(false);
            super.onTouchEvent(textView, spannable, event);
        }
        mPressedSpan = null;
        Selection.removeSelection(spannable);
    }
    return true;
}

private TouchableSpan getPressedSpan(TextView textView, Spannable spannable, MotionEvent
        event) {

    int x = (int) event.getX();
    int y = (int) event.getY();

    x -= textView.getTotalPaddingLeft();
    y -= textView.getTotalPaddingTop();

    x += textView.getScrollX();
    y += textView.getScrollY();

    Layout layout = textView.getLayout();
    int line = layout.getLineForVertical(y);
    int off = layout.getOffsetForHorizontal(line, x);

    TouchableSpan[] link = spannable.getSpans(off, off, TouchableSpan.class);
    TouchableSpan touchedSpan = null;
    if (link.length > 0) {
        touchedSpan = link[0];
    }
    return touchedSpan;
}

 }
© www.soinside.com 2019 - 2024. All rights reserved.