富文本
private void initText() {
SpannableStringBuilder style = new SpannableStringBuilder();
//设置文字
style.append("注册即是同意见道的用户协议和隐私政策");
//下划线
UnderlineSpan underlineSpan = new UnderlineSpan();
style.setSpan(underlineSpan,9,13, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
style.setSpan(underlineSpan,14,18,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//点击事件
ClickableSpan clickableSpan=new ClickableSpan() {
@Override
public void onClick(@NonNull View pView) {
showToast("点击了");
}
};
ClickableSpan clickableSpan1=new ClickableSpan() {
@Override
public void onClick(@NonNull View pView) {
showToast("点击了2");
}
};
style.setSpan(clickableSpan,9,13,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
style.setSpan(clickableSpan1,14,18,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//设置部分颜色
ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#FF0000"));
ForegroundColorSpan foregroundColorSpan1 = new ForegroundColorSpan(Color.parseColor("#FF0000"));
style.setSpan(foregroundColorSpan,9,13,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
style.setSpan(foregroundColorSpan1,14,18,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//设置背景颜色
BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.parseColor("#FFFFFF"));
BackgroundColorSpan backgroundColorSpan1 = new BackgroundColorSpan(Color.parseColor("#FFFFFF"));
style.setSpan(backgroundColorSpan,9,13,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
style.setSpan(backgroundColorSpan1,14,18,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//配置给TextView
mText.setMovementMethod(LinkMovementMethod.getInstance());
mText.setText(style);
}