Jetpack Compose中禁用但可点击的开关

Disabled but clickable switch in jetpack compose(Jetpack Compose中禁用但可点击的开关)

本文介绍了Jetpack Compose中禁用但可点击的开关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个禁用的开关,但我仍然希望它可以点击。我尝试添加clickable修饰符,但当按下开关本身时,它仍然不会注册点击

Switch(
    modifier = Modifier
        .clickable(
            enabled = myBool,
            onClick = onSwitch,
            indication = null,
            interactionSource = remember { MutableInteractionSource() },
        )
        .constrainAs(actionButton) {
            top.linkTo(text.top)
            end.linkTo(parent.end)
        },
    interactionSource = interactionSource,
    checked = switchChecked,
    enabled = if (myBool) false else actionButtonEnabled,
    onSwitch = onSwitch,
)

如何使其可点击,就像启用了它一样?

推荐答案

传递paidUsertrue将在单击TextSwitch时切换开关。

paidUser传递为false将允许您导航到所需的屏幕。

@Composable
fun DisabledClickableSwitch1(
    paidUser: Boolean,
) {
    var checked by remember {
        mutableStateOf(false)
    }
    val onCheckedChange = {
        if (paidUser) {
            checked = !checked
        } else {
            // Navigate to the require screen to show info about the paid feature here
            Log.e("Test", "Navigate from here")
        }
    }
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .clickable {
                onCheckedChange()
            }
            .padding(16.dp),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.SpaceBetween,
    ) {
        Text("Enable Paid Feature")
        Switch(
            checked = checked,
            enabled = paidUser,
            onCheckedChange = null,
        )
    }
}
标签Text是可选的。如果不需要,可以删除。

这篇关于Jetpack Compose中禁用但可点击的开关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Jetpack Compose中禁用但可点击的开关

基础教程推荐