[英]Is there a Jetpack Compose equivalent of A password field with the password visible to the user?
When using XML to create the UI.使用 XML 创建 UI 时。 There is an option for a password field with the password visible to the user.
有一个密码字段选项,密码对用户可见。 All the developer have to do is set the
inputType = TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
开发人员所要做的就是设置
inputType = TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
In Jetpack Compose there is the option to create a textField().在 Jetpack Compose 中,可以选择创建一个 textField()。 Then pass in
visualTransformation = PasswordVisualTransformation()
to make the typing turn into dots.然后传入
visualTransformation = PasswordVisualTransformation()
使输入变成点。 However, it does not preview the letters for a few seconds before turning into dots like how it was with XML.但是,它在变成点之前不会预览字母几秒钟,就像 XML 一样。
Was wondering if there is an equivalent jetpack compose function of a password field with the password visible to the user for a few seconds before it turns into a dot.想知道是否有一个等效的喷气背包组成 function 的密码字段,密码对用户可见几秒钟,然后变成一个点。
Thank you谢谢
The inputType
configures the keyboard type that is shown, acceptable characters and appearance of the edit text. inputType
配置显示的键盘类型、可接受的字符和编辑文本的外观。
With 1.0.0
to have a Password field you can use a TextField
with the KeyboardType.Password
:使用
1.0.0
有一个 Password 字段,您可以将TextField
与KeyboardType.Password
一起使用:
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password)
Check also this ticket for futher configuration.还要检查此票以进行进一步配置。
To use a Password field with visualTransformation(mask character used instead of original text):要使用带有 visualTransformation 的密码字段(使用掩码字符而不是原始文本):
var password by rememberSaveable { mutableStateOf("") }
TextField(
value = password,
onValueChange = { password = it },
label = { Text("Enter password") },
visualTransformation = PasswordVisualTransformation(),
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password)
)
To use a password field visible to the user, just remove the visualTransformation (and use the default VisualTransformation.None
):要使用用户可见的密码字段,只需删除 visualTransformation (并使用默认的
VisualTransformation.None
):
var password by rememberSaveable { mutableStateOf("") }
TextField(
value = password,
onValueChange = { password = it },
label = { Text("Enter password") },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password)
)
If you want to switch between the two options:如果要在两个选项之间切换:
var passwordVisibility by remember { mutableStateOf(false) }
TextField(
//...
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
visualTransformation = if (passwordVisibility) VisualTransformation.None else PasswordVisualTransformation(),
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.