Android TextView局部文字颜色不一样,并实现点击事件

//设置文字变色并可点击
val tip = "您的心愿商品需要1090个心愿值,需要109 个朋友帮忙助愿, 您也可以切换到心愿大厅去寻找简单容易获得的商品"
val textClickable = "切换到心愿大厅"
val tvTip: TextView = contentView.findViewById(R.id.tv_tip)
val ssb = SpannableStringBuilder()
ssb.append(tip)
val clickableSpan: ClickableSpan = object : ClickableSpan() {
    override fun onClick(widget: View) {
        Toast.makeText(view.context, "触发点击事件!", Toast.LENGTH_SHORT).show()
    }
}
val start = tip.indexOf(textClickable)

ssb.setSpan(clickableSpan, start, start + 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
val foregroundColorSpan = ForegroundColorSpan(Color.parseColor("#F0533A"))
ssb.setSpan(foregroundColorSpan, start, start + 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
tvTip.movementMethod = LinkMovementMethod.getInstance()
tvTip.text = ssb

你可能感兴趣的:(Kotlin,Android,android,kotlin)