如何解决Jetpack Compose State List Drawable 等价物
在 Jetpack Compose 中获得 StateListDrawable
功能的最佳方法是什么?
解决方法
您可以使用 InteractionSource
更改组件在不同状态下的显示方式,例如按下、拖动或聚焦组件时。
类似于:
val interactionSource = remember { MutableInteractionSource() }
val isFocused by interactionSource.collectIsFocusedAsState()
//Define the textColor,the icon and the iconTint if the field is focused or not
val textColor = if (isFocused) Color.Black else Color.Red
val icon = if (isFocused) Icons.Filled.Add else Icons.Filled.Share
val iconTint = if (isFocused) Color.Black else Color.Red
TextField(
value = text,onValueChange = {text = it},interactionSource = interactionSource,leadingIcon = {Icon( icon,"contentDescription")},colors = TextFieldDefaults.textFieldColors(
textColor = textColor,leadingIconColor = iconTint
),)
或
val isPressed by interactionSource.collectIsPressedAsState()
val backgroundColor = if (isPressed) Color.Blue else Color.Red
Button(
interactionSource = interactionSource,colors = ButtonDefaults.buttonColors(backgroundColor= backgroundColor),onClick = {}
){
Text("Button")
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。