1,需求
zxndg.png实现文字变色,以及局部文字的点击
2,实现代码
class LoginAgreementFragDg : DialogFragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_login_agreement_frag_dg, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
tvAgree.setOnClickListener {
}
tvDissent.setOnClickListener {
dismiss()
}
isCancelable = false
dialog?.window?.setBackgroundDrawableResource(R.drawable.sp_rec_r40_solid_white)
val content =
"""欢迎使用音娱语音,在您使用时需要连接
数据网络或者WLAN网络。音娱语音非常重
视您的隐私保护和个人信息保护,请认真
阅读<<服务协议>>和<<用户隐私协议>>全
部条款,我们将严格按照各项条款,为您
提供更好的服务。
如您同意此政策,请点击“同意”我们会尽
全力保护您的个人信息安全。"""
val tSpannableStringBuilder = SpannableStringBuilder()
tSpannableStringBuilder.append(content)
val startIndex1 = content.indexOf("<")
val endIndex1 = content.indexOf(">") + 2
val startIndex2 = content.lastIndexOf("<") - 1
val endIndex2 = content.lastIndexOf(">") + 1
//点击1
val serveClickableSpan = object : ClickableSpan() {
override fun onClick(widget: View) {
Toast.makeText(widget.context, "服务协议", Toast.LENGTH_SHORT).show()
}
}
tSpannableStringBuilder.setSpan(
serveClickableSpan,
startIndex1,
endIndex1,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
//颜色1
tSpannableStringBuilder.setSpan(
ForegroundColorSpan(Color.parseColor("#580EFF")),
startIndex1,
endIndex1,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
//点击2
val secretClickableSpan = object : ClickableSpan() {
override fun onClick(widget: View) {
Toast.makeText(widget.context, "用户隐私协议!", Toast.LENGTH_SHORT).show()
}
}
tSpannableStringBuilder.setSpan(
secretClickableSpan,
startIndex2,
endIndex2,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
//颜色2
tSpannableStringBuilder.setSpan(
ForegroundColorSpan(Color.parseColor("#580EFF")),
startIndex2,
endIndex2,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
//配置给TextView
tvLoginAgreementContent.movementMethod = LinkMovementMethod.getInstance()
tvLoginAgreementContent.text = tSpannableStringBuilder
}
companion object {
@JvmStatic
fun newInstance() =
LoginAgreementFragDg()
}
}
网友评论