android - 仅允许在 TextField 中输入字符
问题描述
我试图让我的 TextField 只接受使用keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Text)
as 的字符:
TextField(
value = query3.value,
onValueChange = { newValue ->
query3.value = newValue
},
singleLine = true,
label = {
Text(
"Bank name",
color = colorResource(id = R.color.bright_green),
fontFamily = FontFamily(Font(R.font.poppins_regular)),
fontSize = with(LocalDensity.current) { dimensionResource(id =
R.dimen._12ssp).toSp() },
)
},
interactionSource = interactionSource,
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Text),
.
.
.
但是软键盘仍然允许输入数字。如何使软键盘只允许输入字符?
解决方案
您需要过滤掉文本字段回调中的数字,
TextField(
value = query3.value,
onValueChange = { newValue ->
query3.value = newValue.filter { !it.isDigit() }
},
singleLine = true,
推荐阅读
- python - 将元组返回到字符串中,为什么它只返回元组列表的第一个索引?
- python - 从 jinja 模板中提取变量
- python - 在 Kivy 中使用 Build 时出现黑屏
- shell - grep 中包含的空文件:忽略它们或使用参数/管道将它们过滤掉更有效?
- python - AttributeError:模块“matplotlib.cbook”在 Networkx 中没有属性“is_numlike”
- twilio - Twilio 函数:如果录制以挂断结束,则忽略录制回调的 TwiML 响应
- python - Python3、FastAPI 和 Firebase:无法从 firebase 成功执行 ref.get()
- flutter - 使用 Flutter 和 Firebase 从 Facebook 登录应用程序中提取数据
- python - 如何在不使用多个进程的情况下将 python 对象保存在 RAM 中?
- postgresql - TypeORM - JSONB 数组 LIKE 值中的对象