繁体   English   中英

Jetpack Compose State 列出可绘制等效项

[英]Jetpack Compose State List Drawable Equivalent

在 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")
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM