Mr.*_*hie 8 android textfield android-jetpack-compose kotlin-android
我在尝试着:
trailingIcon
项可见。TextField
trailingIcon
其中的文本,并且应该消失。TextField
trailingIcon
trailingIcon
应该出现 并启用此文本清除功能。等等...
我尝试寻找这个问题的解决方案,但大多数都关注“可见trailingIcons
”,而不是我想要实现的。
Phi*_*hov 17
根据文本状态,您可以指定参数null
或实际视图trailingIcon
:
var text by remember { mutableStateOf("") }
val trailingIconView = @Composable {
IconButton(
onClick = {
text = ""
},
) {
Icon(
Icons.Default.Clear,
contentDescription = "",
tint = Color.Black
)
}
}
TextField(
value = text,
onValueChange = { text = it },
trailingIcon = if (text.isNotBlank()) trailingIconView else null,
)
Run Code Online (Sandbox Code Playgroud)
您可以添加条件以使trailingIcon
.
就像是:
var text by remember { mutableStateOf("") }
val isVisible by remember {
derivedStateOf {
text.isNotBlank()
}
}
OutlinedTextField(
value = text,
onValueChange = {
text = it
},
trailingIcon = {
if (isVisible) {
IconButton(
onClick = { text = "" }
) {
Icon(
imageVector = Icons.Default.Clear,
contentDescription = "Clear"
)
}
}
}
)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
13568 次 |
最近记录: |