1、给文字添加局部变色及点击事件
private SpannableStringBuilder style;
private ClickableSpan clickableSpan;
@Override
protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 给点击此处切换身份文字变色及添加点击事件
style = new SpannableStringBuilder();
//设置文字
style.append("给点击此处切换身份文字变色及添加点击事件");
//设置部分文字点击事件
clickableSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
startActivity(new Intent(ApproveLegalActivity.this, ApproveTypeActivity.class));
finish();
}
@Override
public void updateDrawState(@NonNull TextPaint ds) {
super.updateDrawState(ds);
// 去除下划线
ds.setUnderlineText(false);
}
};
style.setSpan(clickableSpan, 16, 20,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
hintMessage.setText(style);
//设置部分文字颜色
ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#0B8EFF"));
style.setSpan(foregroundColorSpan, 16, 20, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//配置给TextView
hintMessage.setMovementMethod(LinkMovementMethod.getInstance());
hintMessage.setText(style);
}
2、解决内存泄漏问题
@Override
protected void onDestroy() {
super.onDestroy();
if (clickableSpan != null){
style.removeSpan(clickableSpan);
style = null;
hintMessage.setText(null);
}
}