在Android Textview中为不同的超链接提供不同的颜色

在Android Textview中为不同的超链接提供不同的颜色,android,colors,hyperlink,textview,Android,Colors,Hyperlink,Textview,我知道我可以通过设置以下内容在textview级别更改超链接的颜色: yourText.setLinkTextColor(Color.RED); 在Textview中,我的应用程序中可以有多个超链接。我想为每个超链接文本定义不同的颜色。使用 不幸无法工作。 有什么想法吗? 谢谢 解释 ss.setSpan(clickableSpan, 13, 25, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 这将使 使用条款 可点击。类似地,第二个跨度将使 隐私政策 可在我的示

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

yourText.setLinkTextColor(Color.RED);
在Textview中,我的应用程序中可以有多个超链接。我想为每个超链接文本定义不同的颜色。使用
不幸无法工作。

有什么想法吗?
谢谢

解释

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

使用条款

可点击。类似地,第二个跨度将使

隐私政策

可在我的示例字符串中单击。您可以根据需要使用
normal color
touchColor
pressedColor

TouchableSpan.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);
}
}
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;
}

 }
LinkTouchMovementMethod.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);
}
}
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;
}

 }

如何设置文本视图的超链接?Linkify或Html.fromHtml()您将需要使用spannableStringtouchableSpan@MeliX:我使用Html.fromHtml()@karandeep singh:你能提供详细信息吗?谢谢karandeep singh!我在字符串中使用html标记(如粗体、斜体、颜色,还有“a href”)。查看您的解决方案,我必须将“a href”-链接转换为setspan调用。其他html标签呢?