jetpack compose 中的密码可见性切换

问题描述 投票:0回答:1

我正在尝试在jetpack中实现可见性切换,但是在我单击按钮后甚至在身份验证之前密码就可见

var isAuthenticated by remember {
        mutableStateOf(false)
    } 
var show_pass by remember {
        mutableStateOf(false)
    }

IconButton(onClick = {
                        if (!isAuthenticated){
                            promptManager.showBiometricPrompt("Authentication required", "Confirm it's you!")
                            if (biometricResult is BiometricResult.AuthenticationSuccess) {
                                isAuthenticated = true
                                show_pass = true
                            }
                            else{
                                isAuthenticated = false
                                show_pass = false
                            }
                        }
                        else{
                            isAuthenticated = false
                            show_pass = false
                        }
                    }) {
                        Icon(
                            painter = painterResource(id = if (show_pass) R.drawable.eye_alt_svgrepo_com else R.drawable.eye_slash_alt_svgrepo_com),
                            contentDescription = null, modifier = Modifier.size(24.dp)
                        )
                    }

kotlin android-jetpack-compose passwords jetpack
1个回答
0
投票

您需要对密码字段应用“visualTransform”。

val visualTransformation = if (show_pass) {
    VisualTransformation.None
} else {
    PasswordVisualTransformation()
}

这可能如下所示:

OutlinedTextField(
    value = value,
    onValueChange = onValueChange,
    label = label,
    placeholder = placeholder,
    visualTransformation = visualTransformation,
    singleLine = true,
    keyboardActions = keyboardActions,
    keyboardOptions = keyboardOptions,
    isError = isError,
    modifier = modifier,
    trailingIcon = {
        IconButton(onClick = { show_pass = !showPass }) {
            Image(
                imageVector = visibility,
                contentDescription = contentDescription,
                colorFilter = ColorFilter.tint(visibilityColor)
            )
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.