private fun setText() {

//拼接字符串

val spanBuilder = SpannableStringBuilder("同意")

/**

* 服务协议

*/

var span = SpannableString("服务协议")

//服务协议点击事件

span.setSpan(object : ClickableSpan() {

override fun onClick(widget: View) {

Toast.makeText(this@LoginActivity,"服务协议",Toast.LENGTH_SHORT).show()

}

}, 0, span.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

//设置颜色、下划线

span.setSpan(

ForegroundColorSpan(Color.parseColor("#D1894A")),

0,

span.length,

Spanned.SPAN_EXCLUSIVE_EXCLUSIVE

)

spanBuilder.append(span)

spanBuilder.append("与")

/**

* 隐私政策

*/

span = SpannableString("隐私政策")

span.setSpan(object : ClickableSpan() {

override fun onClick(widget: View) {

Toast.makeText(this@LoginActivity,"隐私政策",Toast.LENGTH_SHORT).show()

}

}, 0, span.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

span.setSpan(

ForegroundColorSpan(Color.parseColor("#D1894A")),

0,

span.length,

Spanned.SPAN_EXCLUSIVE_EXCLUSIVE

)

spanBuilder.append(span)

// 赋值给TextView

tv_service_agreement.movementMethod = LinkMovementMethod.getInstance()

tv_service_agreement.text = spanBuilder

//设置高亮颜色透明,因为点击会变色

tv_service_agreement.highlightColor = ContextCompat.getColor(applicationContext, R.color.transparent)

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐