您可以使用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 "")
}
)
结果是
编辑
如果你想在获得焦点时隐藏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")
}
}
)