4
  • Product: Android App
  • Programming language: kotlin

When using XML to create the 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

In Jetpack Compose there is the option to create a textField(). Then pass in visualTransformation = PasswordVisualTransformation() to make the typing turn into dots. However, it does not preview the letters for a few seconds before turning into dots like how it was with 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.

Thank you

4

1 回答 1

9

配置显示inputType的键盘类型、可接受的字符和编辑文本的外观。
有了1.0.0 Password 字段,您可以使用TextFielda KeyboardType.Password

keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password)

还要检查此票以进行进一步配置。

要使用带有 visualTransformation 的密码字段(使用掩码字符而不是原始文本):

var password by rememberSaveable { mutableStateOf("") }
TextField(
    value = password,
    onValueChange = { password = it },
    label = { Text("Enter password") },
    visualTransformation = PasswordVisualTransformation(),
    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password)
)

要使用用户可见的密码字段,只需删除 visualTransformation (并使用默认值VisualTransformation.None):

var password by rememberSaveable { mutableStateOf("") }
TextField(
    value = password,
    onValueChange = { password = it },
    label = { Text("Enter password") },
    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password)
)

如果要在两个选项之间切换:

var passwordVisibility by remember { mutableStateOf(false) }

TextField(
   //...
   keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
   visualTransformation = if (passwordVisibility) VisualTransformation.None else PasswordVisualTransformation(),
)
于 2021-04-11T19:00:42.263 回答