一、介绍
在首次进入app 或者需要登录时,往往需要提示用户同意协议服务协议与隐私协议
两个都可以点击进入条款页面
一般做法为三个TextView进行拼接,但是遇到这样的需求时
只能用一个TextView来实现了。。那么。。。
二、实现方式
设置 SpannableStringBuilder
private fun setSpannableText(
style : SpannableStringBuilder
,start:Int,end:Int
,start2:Int,end2:Int): SpannableStringBuilder{
//设置服务协议点击事件
val serviceAgreement: ClickableSpan = object : ClickableSpan() {
override fun onClick(widget: View) {
//往服务页面跳转
}
override fun updateDrawState(ds: TextPaint) {
super.updateDrawState(ds)
ds.isUnderlineText = false
ds.color = ContextCompat.getColor(applicationContext,R.color.color_009af1)
}
}
//设置隐私政策点击事件
val privacyPolicy: ClickableSpan = object : ClickableSpan() {
override fun onClick(widget: View) {
//往服务页面跳转
}
override fun updateDrawState(ds: TextPaint) {
super.updateDrawState(ds)
ds.isUnderlineText = false
ds.color = ContextCompat.getColor(applicationContext,R.color.color_009af1)
}
}
style.setSpan(serviceAgreement, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
style.setSpan(privacyPolicy, start2, end2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
return style
}
最后直接往TextView里设置即可
var style = SpannableStringBuilder()
style.append("我已阅读并同意《用户服务协议》及《隐私政策》")
style = setSpannableText(style,7,15,16,22)
//这一行必须要
readAgree.movementMethod = LinkMovementMethod.getInstance()
//赋值
readAgree.text = style
//设置高亮颜色透明,因为点击会变色
readAgree.highlightColor = ContextCompat.getColor(applicationContext,R.color.transparent)
结束结束。。xml就是普通的TextView