繁体   English   中英

如何检测 Jetpack Compose 中是否按下了按钮?

[英]How to detect that a button is pressed in Jetpack Compose?

我尝试获得一个按钮,该按钮在点击时具有边框半径变化的酷炫效果(例如在 Android 12 默认计算器应用程序中),同时还保持按钮的波纹效果。

我认为会起作用的是:

var pressed by remember { mutableStateOf(false) }
val borderRadius by animateDpAsState(
    if (pressed) 10.0.dp
    else 20.0.dp
)
val buttonTitle = "uniqueButton"

Button(
    onClick = {
        // some on click stuff
    },
    shape = RoundedCornerShape(borderRadius),
    modifier = Modifier
        .pointerInput(buttonTitle) {
            detectTapGestures(
                onPress = {
                    pressed = true
                    tryAwaitRelease()
                    pressed = false
                }
            )
        }
) {
    Text(text = buttonTitle)
}

但是传递给onPress的 function 中的代码永远不会执行。 据我了解应该是,当点击或按下按钮时。 我的错误在哪里,我误解 了文档中的内容吗?

大多数 Compose 控件都有用于此目的的interactionSource参数。 以下是您可以使用它的方法:

val interactionSource = remember { MutableInteractionSource() }
val pressed by interactionSource.collectIsPressedAsState()
Button(
    onClick = {},
    interactionSource = interactionSource,
) {

}

暂无
暂无

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

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