kotlin - PasswordVisualTransformation 将输入键转换为星号
问题描述
我正在PasswordInput
使用 Jetpack compose 实现可组合,这实际上是 Android 开发者网站提供的示例之一:
@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”,长度为五个字符。它显示五个星号。
但是如果用户在那里按下回车键,密码字段就会变成六个字符。
问题:
- 为什么这样做?
- 如何修改它以不这样做?
解决方案
我要回答我的问题。它需要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)
)
}
推荐阅读
- ios - 为什么在 SwiftUI 中不按顺序执行状态更改?
- flutter - 在颤动中使用 PUT 请求更新 JSON 列表对象
- android - 当有工具栏或底部导航栏时,我应该使用哪种布局来使视图覆盖整个剩余屏幕?
- python - Jupyter Notebook - 无法加载已安装的包
- latex - biblatex:拆分按名称连接为“包”的参考书目条目
- java - 如何使用java展平动态yaml文件
- kotlin - 如何从 Kotlin 的 Spinner 中获取选定的项目?
- node.js - React/Node 项目中的 Socket.io 问题
- angular - Angular Fullcalendar 不刷新事件
- python - Remove specific references like author from scientific publication