个别文字变色、可以点击跳转(适用于用户协议隐私政策)

该文章展示了如何在Android应用中使用SpannableStringBuilder和ClickableSpan来创建可点击的文本段落,并通过Intent实现在不同活动间的跳转。同时,通过设置TextPaint去除下划线并改变颜色,提升用户体验。
摘要由CSDN通过智能技术生成
 //获取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" />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值