【问题标题】:PasswordVisualTransformation converts enter key to asteriskPasswordVisualTransformation 将输入键转换为星号
【发布时间】:2021-04-09 19:54:34
【问题描述】:

我正在使用 Jetpack compose 实现一个 PasswordInput 可组合,它是 Android 开发者网站提供的 literally one of the examples

@Composable
fun PasswordInput() {
    var password by remember { mutableStateOf("admin") }

    TextField(
        value = password,
        onValueChange = { password = it },
        label = { Text(text = stringResource(id = R.string.prompt_password)) },
        modifier = Modifier
            .fillMaxWidth(),
        visualTransformation = PasswordVisualTransformation(),
        keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password)
    )
}

这里我的默认密码为“admin”,长度为五个字符。它显示五个星号。

但是如果用户在那里按下回车键,密码字段就会变成六个字符。

问题:

  1. 为什么要这样做?
  2. 如何修改不这样做?

【问题讨论】:

    标签: kotlin android-jetpack-compose


    【解决方案1】:

    我将回答我的问题。它需要singleLine = true,文档没有提到。

    固定代码:

    @Composable
    fun PasswordInput() {
        var password by rememberSaveable { mutableStateOf("") }
    
        TextField(
            value = password,
            onValueChange = { password = it },
            maxLines = 1,
            singleLine = true,
            label = { Text(text = stringResource(id = R.string.prompt_password)) },
            modifier = Modifier
                .fillMaxWidth(),
            visualTransformation = PasswordVisualTransformation(),
            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password)
        )
    }
    

    Source

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-18
      • 1970-01-01
      • 1970-01-01
      • 2019-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-22
      相关资源
      最近更新 更多