04. Compose Text

01. Compose 可组合组件之Row And Column

02. Compose 可组合组件之 属性 modifier

03. Compose 可组合组件之Card 图片

04. Compose Text

class FontActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            //创建字体
            val fontFamily = FontFamily(
                Font(R.font.opensans_bold, FontWeight.Bold),
                Font(R.font.opensans_light, FontWeight.Thin),
                Font(R.font.opensans_semibold, FontWeight.Normal),
            )

            val state = rememberScrollState()
            Column(
                modifier = Modifier
                    .padding(16.dp)
                    .fillMaxSize()
                    .verticalScroll(state = state, enabled = true)
            ) {
                Text(text = "Android Jetpack Compose", color = Co_333333)
                Text(stringResource(R.string.app_name), color = Co_333333)
                //可选文字
                SelectionContainer {
                    Text("This text is selectable")
                }

                /**
                 * 666 可以监听到 第几个字符的点击事件
                 */
                ClickableText(
                    text = AnnotatedString("Click Me"),
                    style = TextStyle(fontSize = 35.sp),
                    onClick = { offset ->
                        Log.d("ClickableText", "$offset -th character is clicked.")
                    }
                )

                val annotatedText = buildAnnotatedString {
                    append("Click ")
                    // We attach this *URL* annotation to the following content
                    // until `pop()` is called
                    pushStringAnnotation(
                        tag = "URL",
                        annotation = "https://developer.android.com"
                    )
                    withStyle(
                        style = SpanStyle(
                            color = Color.Blue,
                            fontWeight = FontWeight.Bold
                        )
                    ) {
                        append("here")
                    }
                    pop()
                }

                ClickableText(
                    text = annotatedText,
                    style = TextStyle(fontSize = 35.sp),
                    onClick = { offset ->
                        // We check if there is an *URL* annotation attached to the text
                        // at the clicked position
                        annotatedText.getStringAnnotations(
                            tag = "URL", start = offset,
                            end = offset
                        )
                            .firstOrNull()?.let { annotation ->
                                // If yes, we log its value
                                Log.d("Clicked URL", annotation.item)
                            }
                    }
                )



                Text(
                    "Android Jetpack Compose Android Jetpack Compose Android Jetpack Compose Android Jetpack Compose",
                    fontSize = 20.sp,
                    fontWeight = FontWeight.Thin,
                    fontStyle = FontStyle.Italic,
                    fontFamily = fontFamily,
                    textAlign = TextAlign.Center,
                    letterSpacing = TextUnit.Unspecified,
                    overflow = TextOverflow.Ellipsis,
                    maxLines = 1
                )
                Text(
                    "Android Jetpack Compose Android Jetpack Compose Android Jetpack Compose Android Jetpack Compose",
                    fontSize = 20.sp,
                    fontWeight = FontWeight.Normal,
                    fontStyle = FontStyle.Italic,
                    fontFamily = fontFamily,
                    textAlign = TextAlign.Center,
                    overflow = TextOverflow.Ellipsis,
                    softWrap = false,
                    maxLines = 1
                )

                Text(
                    "Android Jetpack Compose Android Jetpack Compose Android Jetpack Compose Android Jetpack Compose",
                    fontSize = 20.sp,
                    fontWeight = FontWeight.Bold,
                    fontStyle = FontStyle.Italic,
                    fontFamily = fontFamily,
                    textAlign = TextAlign.Center,
                    overflow = TextOverflow.Ellipsis,
                    maxLines = 1,
                    textDecoration = TextDecoration.LineThrough
                )
                Text(
                    "Android Jetpack Compose Android Jetpack Compose Android Jetpack Compose Android Jetpack Compose",
                    fontSize = 20.sp,
                    fontWeight = FontWeight.Bold,
                    fontStyle = FontStyle.Italic,
                    fontFamily = fontFamily,
                    textAlign = TextAlign.Center,
                    overflow = TextOverflow.Ellipsis,
                    maxLines = 1,
                    textDecoration = TextDecoration.Underline
                )

                Text(
                    "Android Jetpack Compose Android Jetpack Compose Android Jetpack Compose Android Jetpack Compose",
                    fontSize = 20.sp,
                    fontWeight = FontWeight.Bold,
                    fontStyle = FontStyle.Italic,
                    fontFamily = fontFamily,
                    textAlign = TextAlign.Center,
                    overflow = TextOverflow.Ellipsis,
                    maxLines = 1,
                    textDecoration = TextDecoration.None
                )
                Text(
                    text = "Android Jetpack Compose",
                    color = Co_333333,
                    fontSize = 18.sp,
                )

                Text(
//                    text = "Android Jetpack Compose",
                    text = buildAnnotatedString {
                        withStyle(
                            style = SpanStyle(
                                color = Color.Red,
                                fontSize = 30.sp
                            )
                        ) {
                            append("A")
                        }
                        append("ndroid ")
                        withStyle(
                            style = SpanStyle(
                                color = Color.Red,
                                fontSize = 30.sp
                            )
                        ) {
                            append("J")
                        }
                        append("etpack ")
                        withStyle(
                            style = SpanStyle(
                                color = Color.Red,
                                fontSize = 30.sp
                            )
                        ) {
                            append("C")
                        }
                        append("ompose")

                    },
                    color = Co_333333,
                    fontSize = 20.sp,
                    fontFamily = fontFamily,
                    fontWeight = FontWeight.Bold,
//                    fontStyle = FontStyle.Italic,
                    textAlign = TextAlign.Center,
//                    textDecoration = TextDecoration.LineThrough,
//                    textDecoration = TextDecoration.Underline,
                )


                Text(
                    text = buildAnnotatedString {
                        withStyle(
                            style = SpanStyle(
                                fontFamily = fontFamily,
                                fontWeight = FontWeight.Normal,
                                color = Color.Black,
                                fontSize = 30.sp
                            )
                        ) {
                            append("30,000")
                        }
                        append("¥")
                    },
                    color = Color.Red,
                    fontSize = 14.sp,
                    fontFamily = fontFamily,
                    fontWeight = FontWeight.Thin,
                    textAlign = TextAlign.Center,
                )

                var text by remember { mutableStateOf("") }

                OutlinedTextField(
                    //一下两个同时设置才可以起作用
                    visualTransformation = PasswordVisualTransformation(),
                    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
                    value = text,
                    textStyle = LocalTextStyle.current,
                    onValueChange = {
                        text = it
                    },
                    label = { Text("请输入密码") }
//                    keyboardActions = KeyboardActions()
                )

                Spacer(modifier = Modifier.padding(10.dp))

                var content by remember { mutableStateOf("Compose") }

                TextField(
                    value = content,
                    onValueChange = { content = it },
                    label = { Text("Label") }
                )
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值