一万年来谁著史,三千里外欲封侯。这篇文章主要讲述在Android Textview中为不同的超链接使用不同的颜色相关的知识,希望能为你提供帮助。
我知道我可以通过设置更改textview级别的超链接颜色:
yourText.setLinkTextColor(Color.RED);
在Textview中,我的应用程序中可以有多个超链接。我想为每个超链接文本定义不同的颜色。不幸的是,使用
<
font color ...>
does不起作用。有任何想法吗?
谢谢!答案
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 color
,touchColor
和pressedColor
。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);
}
}
【在Android Textview中为不同的超链接使用不同的颜色】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;
} }
推荐阅读
- 什么是Android提示的颜色代码()
- 如何使用onRequestPermissionsResult处理React Native Android模块中的权限()
- 在RecyclerView-AndroidX中的项目之间添加不同的布局
- Android计时器更新文本视图(UI)
- 在Android Studio中添加延迟计时器
- 如何使用android延迟不扩展或实现Thread或Runnable
- 无法识别的选择器发送到AppDelegate中的实例
- android - 在给定的时间后只调用一次通知
- 如何插入带有镶木地板格式和SNAPPY压缩的蜂巢表()