【问题标题】:Have different colors for different hyperlinks within an Android TextviewAndroid Textview 中的不同超链接有不同的颜色
【发布时间】:2018-08-10 18:27:11
【问题描述】:

我知道我可以通过设置在文本视图级别更改超链接的颜色:

yourText.setLinkTextColor(Color.RED);

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

有什么想法吗?
谢谢!

【问题讨论】:

  • 如何设置TextView的超链接? Linkify 或 Html.fromHtml()
  • 你需要使用 spannableString 和 touchableSpan
  • @MeliX:我使用 Html.fromHtml()
  • @karandeep singh:您能否提供详细信息?

标签: android colors hyperlink textview


【解决方案1】:
 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

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

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

 }

【讨论】:

  • 感谢卡兰迪普·辛格!我在我的字符串中使用 html 标记(如粗体、斜体、颜色,还有“a href”)。查看您的解决方案,我必须将“a href”链接转换为 setspan 调用。其他html标签呢?
猜你喜欢
  • 1970-01-01
  • 2018-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-13
  • 1970-01-01
  • 1970-01-01
  • 2021-09-09
相关资源
最近更新 更多