在JetPack组成的密码字段中使用切换

发布于 2025-02-11 20:26:41 字数 2193 浏览 2 评论 0原文

我正在尝试在密码字段中使用切换。到目前为止,这是我的代码,但由于某种原因,它无法使用。如何像此图像一样实现切换密码?非常感谢!

图片: https://gyazo e

    Column(
        modifier = Modifier.padding(20.dp),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally

    ) {

        val username = remember { mutableStateOf(TextFieldValue()) }
        val password = remember { mutableStateOf("") }
        var revealPassword: MutableState<Boolean> =
            remember { mutableStateOf(false) } // To reveal the password with toggle


        Text(text = "Sign in", style = TextStyle(fontSize = 40.sp, fontFamily = FontFamily.Cursive))
        Text(
            text = "here",
            style = TextStyle(fontSize = 40.sp, fontFamily = FontFamily.Cursive)
        )
        // PASSWORD
        Spacer(modifier = Modifier.height(20.dp))
        OutlinedTextField(
            value = password.value,
            onValueChange = { newText ->
                password.value = newText
            },

            visualTransformation = if (revealPassword.value) {
                VisualTransformation.None
            } else {
                PasswordVisualTransformation()
            },
            trailingIcon = {
                if (revealPassword.value) {
                    IconButton(onClick = {
         (I changed this!! -->) revealPassword.Value = false
                    }) { Icon(imageVector = Icons.Filled.Visibility, contentDescription = null) }
                } else {
                  IconButton(onClick = { 
(I changed this!! -->) revealPassword.Value = true }) {
                        Icon(imageVector = Icons.Filled.VisibilityOff, contentDescription = null)
                    }
                }
            },

            label = { Text(text = "Password") },
            singleLine = true,
            leadingIcon = { Icon(imageVector = Icons.Default.Lock, contentDescription = null) },
            modifier = Modifier
                .fillMaxWidth(180F),
            
            )

I am trying to use toggle in a password field. This is my code so far but for some reason it wont work. How can I achieve toggling a password as in this image? Much appreciated!

Image: https://gyazo.com/5ad35b44dc955e0846c68f61ec9630b0

    Column(
        modifier = Modifier.padding(20.dp),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally

    ) {

        val username = remember { mutableStateOf(TextFieldValue()) }
        val password = remember { mutableStateOf("") }
        var revealPassword: MutableState<Boolean> =
            remember { mutableStateOf(false) } // To reveal the password with toggle


        Text(text = "Sign in", style = TextStyle(fontSize = 40.sp, fontFamily = FontFamily.Cursive))
        Text(
            text = "here",
            style = TextStyle(fontSize = 40.sp, fontFamily = FontFamily.Cursive)
        )
        // PASSWORD
        Spacer(modifier = Modifier.height(20.dp))
        OutlinedTextField(
            value = password.value,
            onValueChange = { newText ->
                password.value = newText
            },

            visualTransformation = if (revealPassword.value) {
                VisualTransformation.None
            } else {
                PasswordVisualTransformation()
            },
            trailingIcon = {
                if (revealPassword.value) {
                    IconButton(onClick = {
         (I changed this!! -->) revealPassword.Value = false
                    }) { Icon(imageVector = Icons.Filled.Visibility, contentDescription = null) }
                } else {
                  IconButton(onClick = { 
(I changed this!! -->) revealPassword.Value = true }) {
                        Icon(imageVector = Icons.Filled.VisibilityOff, contentDescription = null)
                    }
                }
            },

            label = { Text(text = "Password") },
            singleLine = true,
            leadingIcon = { Icon(imageVector = Icons.Default.Lock, contentDescription = null) },
            modifier = Modifier
                .fillMaxWidth(180F),
            
            )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无尽的现实 2025-02-18 20:26:41

您已经在两个onclick()中给出了RelevePassword = true

将第一个更改为ReplayPassword = false

完成代码

@Composable
fun TogglePassword() {
    val password = remember {
        mutableStateOf("")
    }
    var revealPassword: MutableState<Boolean> = remember {
        mutableStateOf(false)
    } // To reveal the password with toggle
    OutlinedTextField(
        value = password.value,
        onValueChange = { newText ->
            password.value = newText
        },
        visualTransformation = if (revealPassword.value) {
            VisualTransformation.None
        } else {
            PasswordVisualTransformation()
        },
        trailingIcon = {
            if (revealPassword.value) {
                IconButton(
                    onClick = {
                        revealPassword.value = false
                    },
                ) {
                    Icon(imageVector = Icons.Filled.Visibility, contentDescription = null)
                }
            } else {
                IconButton(
                    onClick = {
                        revealPassword.value = true
                    },
                ) {

                    Icon(imageVector = Icons.Filled.VisibilityOff, contentDescription = null)
                }
            }
        },
        label = {
            Text(text = "Password")
        },
        singleLine = true,
        leadingIcon = {
            Icon(imageVector = Icons.Default.Lock, contentDescription = null)
        },
        modifier = Modifier
            .fillMaxWidth()
            .padding(16.dp),
    )
}

You have given revealPassword = true in both onClick().

Change the first one to revealPassword = false

Complete code

@Composable
fun TogglePassword() {
    val password = remember {
        mutableStateOf("")
    }
    var revealPassword: MutableState<Boolean> = remember {
        mutableStateOf(false)
    } // To reveal the password with toggle
    OutlinedTextField(
        value = password.value,
        onValueChange = { newText ->
            password.value = newText
        },
        visualTransformation = if (revealPassword.value) {
            VisualTransformation.None
        } else {
            PasswordVisualTransformation()
        },
        trailingIcon = {
            if (revealPassword.value) {
                IconButton(
                    onClick = {
                        revealPassword.value = false
                    },
                ) {
                    Icon(imageVector = Icons.Filled.Visibility, contentDescription = null)
                }
            } else {
                IconButton(
                    onClick = {
                        revealPassword.value = true
                    },
                ) {

                    Icon(imageVector = Icons.Filled.VisibilityOff, contentDescription = null)
                }
            }
        },
        label = {
            Text(text = "Password")
        },
        singleLine = true,
        leadingIcon = {
            Icon(imageVector = Icons.Default.Lock, contentDescription = null)
        },
        modifier = Modifier
            .fillMaxWidth()
            .padding(16.dp),
    )
}
提笔书几行 2025-02-18 20:26:41

您将布尔人设置为错误。
而且我认为控制上面的VisualTransformation是更干净的。

@Composable
fun PasswordTextField() {
    var masked by remember {
        mutableStateOf(false)
    }
    var password by remember {
        mutableStateOf("")
    }
    val visualTransformation by remember(masked) {
        if (masked)
            mutableStateOf(PasswordVisualTransformation())
        else
            mutableStateOf(VisualTransformation.None)
    }
            OutlinedTextField(
                value = password,
                onValueChange = { newText ->
                    password = newText
                },
                visualTransformation = visualTransformation,
                trailingIcon = {
                    if (masked) {
                        IconButton(onClick = {
                            masked = false
                        }) { Icon(imageVector = Icons.Filled.VisibilityOff, contentDescription = null) }
                    } else {
                        IconButton(onClick = { masked = true }) {
                            Icon(imageVector = Icons.Filled.Visibility, contentDescription = null)
                        }
                    }
                },

                label = { Text(text = "Password") },
                singleLine = true,
                modifier = Modifier
                    .fillMaxWidth(.1f),
            )
}

You are setting boolean wrong.
And I think it is cleaner to control VisualTransformation above.

@Composable
fun PasswordTextField() {
    var masked by remember {
        mutableStateOf(false)
    }
    var password by remember {
        mutableStateOf("")
    }
    val visualTransformation by remember(masked) {
        if (masked)
            mutableStateOf(PasswordVisualTransformation())
        else
            mutableStateOf(VisualTransformation.None)
    }
            OutlinedTextField(
                value = password,
                onValueChange = { newText ->
                    password = newText
                },
                visualTransformation = visualTransformation,
                trailingIcon = {
                    if (masked) {
                        IconButton(onClick = {
                            masked = false
                        }) { Icon(imageVector = Icons.Filled.VisibilityOff, contentDescription = null) }
                    } else {
                        IconButton(onClick = { masked = true }) {
                            Icon(imageVector = Icons.Filled.Visibility, contentDescription = null)
                        }
                    }
                },

                label = { Text(text = "Password") },
                singleLine = true,
                modifier = Modifier
                    .fillMaxWidth(.1f),
            )
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文