我是靠谱客的博主 昏睡鞋子,最近开发中收集的这篇文章主要介绍SpannableStringBuilder、SpannableString设置文字样式、设置文案中部分文字样式(颜色、字体样式、点击事件),觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
- SpannableStringBuilder和SpannableString区别:类似于String和StringBuilder一样
- 部分文字添加点击事件以及文字样式修改
/**使用SpannableStringBuilder*/
val str = "今天晚上下雨啦!"
val value1 = "今天"
val start1 = str.indexOf(value1)
val end1 = start1 + value1.length
val value = "下雨"
val start = str.indexOf(value)
val end = start + value.length
val spannableBuild = SpannableStringBuilder(str)
//指定value添加点击事件
spannableBuild.setSpan(object: ClickableSpan(){
override fun onClick(widget: View) {
Toast.makeText(this@MainActivity, value1, Toast.LENGTH_SHORT).show()
}
override fun updateDrawState(ds: TextPaint) {
super.updateDrawState(ds)
ds.isUnderlineText = false
}
},start1, end1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
spannableBuild.setSpan(object: ClickableSpan(){
override fun onClick(widget: View) {
Toast.makeText(this@MainActivity, value, Toast.LENGTH_SHORT).show()
}
override fun updateDrawState(ds: TextPaint) {
super.updateDrawState(ds)
ds.isUnderlineText = false
}
},start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
//设置字体颜色
val spanColor1 = ForegroundColorSpan(resources.getColor(R.color.purple_700, null))
val spanColor2 = ForegroundColorSpan(resources.getColor(R.color.purple_700, null))
spannableBuild.setSpan(spanColor1,start1, end1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
spannableBuild.setSpan(spanColor2,start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
//value斜体加粗 --- Typeface.BOLD:加粗;Typeface.BOLD_ITALIC:斜体加粗
spannableBuild.setSpan(StyleSpan(Typeface.BOLD_ITALIC), start1, end1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
spannableBuild.setSpan(StyleSpan(Typeface.BOLD_ITALIC), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
//设置字体大小
spannableBuild.setSpan(AbsoluteSizeSpan(resources.getDimensionPixelSize(R.dimen.dp_30)),start1,end1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
spannableBuild.setSpan(AbsoluteSizeSpan(resources.getDimensionPixelSize(R.dimen.dp_30)),start,end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
bind.tv02.text = spannableBuild
bind.tv02.movementMethod = LinkMovementMethod.getInstance()
//注意:如果点击事件和字体样式同时添加,点击事件顺序在前,字体样式顺序在后
//注意:添加点击事件时,需对TextView添加:tv.movementMethod = LinkMovementMethod.getInstance()
/**使用SpannableStringBuilder+SpannableString*/
val value = "下雨"
val spannableBuild = SpannableStringBuilder()
val spannableString = SpannableString("下雨")
//指定value添加点击事件
spannableString.setSpan(object: ClickableSpan(){
override fun onClick(widget: View) {
Toast.makeText(this@MainActivity, value, Toast.LENGTH_SHORT).show()
}
override fun updateDrawState(ds: TextPaint) {
super.updateDrawState(ds)
ds.isUnderlineText = false
}
},0, value.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
//设置字体颜色
val spanColor = ForegroundColorSpan(resources.getColor(R.color.purple_700, null))
spannableString.setSpan(spanColor,0, value.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
//value斜体加粗 --- Typeface.BOLD:加粗;Typeface.BOLD_ITALIC:斜体加粗
spannableString.setSpan(StyleSpan(Typeface.BOLD_ITALIC), 0, value.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
//设置字体大小
spannableString.setSpan(AbsoluteSizeSpan(resources.getDimensionPixelSize(R.dimen.dp_30)),0,value.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
spannableBuild.append("今天晚上").append(spannableString).append("啦!")
bind.tv02.text = spannableBuild
bind.tv02.movementMethod = LinkMovementMethod.getInstance()
最后
以上就是昏睡鞋子为你收集整理的SpannableStringBuilder、SpannableString设置文字样式、设置文案中部分文字样式(颜色、字体样式、点击事件)的全部内容,希望文章能够帮你解决SpannableStringBuilder、SpannableString设置文字样式、设置文案中部分文字样式(颜色、字体样式、点击事件)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复