0
点赞
收藏
分享

微信扫一扫

AndroidCompose布局,一个Text写多种字体样式和点击事件

蓝哆啦呀 2022-02-15 阅读 95

@Composable
fun BaseScreen(){
Column(modifier = Modifier
.background(color = colorResource(id = R.color.white)).fillMaxSize(),
verticalArrangement = Arrangement.Center, //垂直居中
horizontalAlignment = Alignment.CenterHorizontally//水平居中
){
val firstText=“9935472821”
val styleText= buildAnnotatedString {
withStyle(
SpanStyle(
color = colorResource(id = R.color.color_333333),
fontSize = 14.sp
)
){
append(“第一行内容\n”)
}
withStyle(
SpanStyle(
color = colorResource(id = R.color.c_2776ff),
fontSize = 18.sp,
background = colorResource(id = R.color.black),
)
) {
pushStringAnnotation(“onClickOne”,annotation = “点击事件一”)
append(“第二行内容\n”)
}
pushStringAnnotation(“onClickTwo”,annotation = firstText)
append(firstText) //默认ClickableText中设置的style样式
}
ClickableText(
text =styleText ,
//TextView中默认样式
style=TextStyle(
color = colorResource(id = R.color.c_1eb184),
fontSize = 20.sp,
fontWeight = FontWeight.Bold
),
onClick ={
//点击事件一
styleText.getStringAnnotations(tag = “onClickOne”,start = it,end = it)
.firstOrNull()?.let { Toast.makeText(applicationContext, “第二行内容”, Toast.LENGTH_SHORT).show() }
//点击事件二
styleText.getStringAnnotations(tag = “onClickTwo”,start = it,end = it)
.firstOrNull()?.let { Toast.makeText(applicationContext, firstText, Toast.LENGTH_SHORT).show(); }
} )
}
}
效果:在这里插入图片描述

举报

相关推荐

0 条评论