如何在文本输入后隐藏或禁用提示标签?

OutlinedTextField(
            value = value,
            modifier = Modifier.fillMaxSize(),
            shape = RoundedCornerShape(8.dp),
            textStyle = TextStyle(fontSize = nFontSize),
            colors = TextFieldDefaults.outlinedTextFieldColors(
                textColor = Color.Black,
                focusedBorderColor = colorResource(id = R.color.inbox_company_red),
                cursorColor = Color.Black),
            //visualTransformation = VisualTransformation.None,
            label = {Text(text = if (value.isEmpty()) stringResource(id = R.string.name" else "")}

enter image description here

推荐答案

您可以使用remember函数隐藏,如下所示:

    var value by remember { mutableStateOf("") 

然后更新你的@Composable,如下所示:

OutlinedTextField(
        value = value,
        onValueChange = {
            value = it
        },
        modifier = Modifier.fillMaxSize(),
        shape = RoundedCornerShape(8.dp),
        textStyle = TextStyle(fontSize = 16.sp),
        colors = TextFieldDefaults.outlinedTextFieldColors(
            textColor = Color.Black,
            focusedBorderColor = Color.Red, // Change this to your desired color
            cursorColor = Color.Black
        ),
        label = {
            Text(text = if (value.isEmpty()) "Hint" else "")
        }
    )

结果是

enter image description here

enter image description here

编辑

如果你想在获得焦点时隐藏label你的@Composable,你可以使用Modifier.onFocusChange查看编辑过的代码

    var value by remember { mutableStateOf("") }
    var isFocused by remember { mutableStateOf(false) }
    var isLabelVisible by remember { mutableStateOf(true) }

    OutlinedTextField(
        value = value,
        onValueChange = {
            value = it
        },
        modifier = Modifier
            .size(width = 200.dp, height = 100.dp)
            .onFocusChanged { focusState ->
                isFocused = focusState.isFocused
                if (focusState.isFocused) {
                    isLabelVisible = false
                } else if (value.isEmpty()) {
                    isLabelVisible = true
                }
            },
        shape = RoundedCornerShape(8.dp),
        textStyle = TextStyle(fontSize = 16.sp),
        colors = TextFieldDefaults.outlinedTextFieldColors(
            textColor = Color.Black,
            focusedBorderColor = Color.Red, 
            cursorColor = Color.Black
        ),
        label = {
            if (isLabelVisible) {
                Text(text = "Hint")
            }
        }
    )

Android相关问答推荐

Android:点击操作栏返回按钮后应用程序无react

Jetpack编写:通过viewModels()vs viewModel View ModernName()'

编写Landscape BoxWithRequests具有错误的高度.(aspectRatio matchHeight约束第一次未按预期工作)

无法在Android中创建通知频道

在Jetpack Compose中,如何判断屏幕是否已重新组合?

我无法在底部导航栏中正确导航-Android底部导航视图

如何制作安卓';s FileProvider在Android 11上使用外部存储+

SDK 33 的问题 - 面向 S+(版本 31 及更高版本)要求在创建 PendingIntent 时指定 FLAG_IMMUTABLE 或 FLAG_MUTABLE 之一

制作圆形SupportMapFragment

将输出写入已发布的 Android 应用程序中的日志(log)文件?

在 kotlin 协同 routine 中,如何将数据范围限定为请求路径(以 MDC 为例)?

使用不同的gradle文件导入外部库

是否可以在 Android 应用程序的 Wifi 设置中为 DNS 服务器设置自定义 IP?

如何在 Jetpack Compose 中对数据类进行 Parcelize

如何仅同步 local_manifest.xml?

Android Transitions API - 在 24-48 小时后停止接收任何更新

没有互联网连接时,Firebase Storage putFile() 永远不会完成

如何在行/列/卡片 compose 中添加左边框

在 Kotlin 客户端应用程序中发送 FCM 推送通知 - Firebase 云消息传递

未解决的参考:getIntentSender / try 在 Jetpack Compose 中获取电话号码时