//获取text的字符串
val spannable = SpannableStringBuilder(tv_user_vip_privacy.text.toString())
//跳转到别的页面
val userClickableSpan = object : ClickableSpan() {
override fun onClick(widget: View) {
var intent = Intent()
intent.setClass(this@VipDetailsMainActivity, UserMainActivity().javaClass)
startActivity(intent)
}
override fun updateDrawState(ds: TextPaint) {
super.updateDrawState(ds)
//去掉下划线
ds.isUnderlineText = false
//变换的颜色
ds.color = getColor(R.color.tab_text_color_selected)
}
}
val privacyClickableSpan = object : ClickableSpan() {
override fun onClick(widget: View) {
var intent = Intent()
intent.setClass(this@VipDetailsMainActivity, PrivacyMainActivity().javaClass)
startActivity(intent)
}
override fun updateDrawState(ds: TextPaint) {
super.updateDrawState(ds)
ds.isUnderlineText = false
ds.color = getColor(R.color.tab_text_color_selected)
}
}
//下标进行变色
spannable.setSpan(userClickableSpan, 78, 96, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
tv_user_vip_privacy.text = spannable
tv_user_vip_privacy.movementMethod = LinkMovementMethod.getInstance()
xml代码
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tv_user_vip_privacy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="13dp"
android:layout_marginEnd="5dp"
android:text="@string/vip_renew_privacy_context"
android:textColor="#A5A5A5"
android:textSize="11sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/appCompatTextView12" />