当在Jetpack Compose中点击按钮时,按钮不会保持透明状态。

huangapple go评论61阅读模式
英文:

Button doesnt stay transparent when its clicked in Jetpack Compose

问题

我有一个Jetpack Compose应用程序,我希望在点击按钮时保持按钮透明,但实际上我看到它暂时变成了灰色。

这是我的按钮代码:

Button(
    onClick = onStartQuiz,
    modifier = Modifier.size(buttonWidth, buttonHeight).align(Alignment.Center).background(Color.Transparent),
    colors = buttonColors(
        containerColor = Color.Transparent,
        contentColor = Color.White, // 如果需要,可以将此更改为所需的文本颜色
        disabledContainerColor = Color.Transparent,
        disabledContentColor = Color.Transparent // 如果有禁用状态并且需要不同的颜色,可以更改此处
    ),
    contentPadding = PaddingValues(0.dp) // 这是可选的,但可以确保按钮保持完全透明
) {
    Text("开始测验")
}

我正在使用Material 3库。

英文:

I have a Jetpack Compose app and I want to make sure the button stays transparent when I click it but instead momentarily I see a gray color of it.

This is my Button

                Button(
                onClick = onStartQuiz,
                modifier = Modifier.size(buttonWidth, buttonHeight).align(Alignment.Center).background(Color.Transparent),
                colors = buttonColors(
                    containerColor = Color.Transparent,
                    contentColor = Color.White, // change this to the desired text color if needed
                    disabledContainerColor = Color.Transparent,
                    disabledContentColor = Color.Transparent // change this if you have a disabled state and want a different color
                ),
                contentPadding = PaddingValues(0.dp) // This is optional, but can help in ensuring the button remains completely transparent
            ) {
                Text("Start Quiz")
            }

I am using material 3 library

答案1

得分: 0

我不认为你可以移除按钮的涟漪效果(即短暂的灰色),因为它是在Button组件内部实现的。

作为解决方法,你可以直接将onClick操作分配给Text组件,使用Modifier.clickable,在这里可以禁用涟漪效果:

Text(
    text = "开始测验",
    textAlign = TextAlign.Center,
    color = Color.White,
    modifier = Modifier
        .size(buttonWidth, buttonHeight)
        .align(Alignment.Center)
        .background(Color.Transparent)
        .clickable(
            interactionSource = remember { MutableInteractionSource() },
            indication = null, // 禁用涟漪效果
            onClick = onStartQuiz
        )
)
英文:

I don't think you can remove the button's ripple effect (i.e. the momentary gray color), as it is internally implemented in the Button composable.

As workaround, you can assign onClick action directly to the Text component, using Modifier.clickable, where the ripple effect can be disabled:

Text(
    text = "Start Quiz",
    textAlign = TextAlign.Center,
    color = Color.White,
    modifier = Modifier
        .size(buttonWidth, buttonHeight)
        .align(Alignment.Center)
        .background(Color.Transparent)
        .clickable(
            interactionSource = remember { MutableInteractionSource() },
            indication = null, // disable ripple effect
            onClick = onStartQuiz
        )
    )

huangapple
  • 本文由 发表于 2023年8月8日 21:19:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76859969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定