實現效果:
亦或者是在彈窗中顯示,都是一樣的。
程式碼比較簡單:
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)
}