仅允许在文本字段中输入字符

Spa*_*tta 1 android textfield android-softkeyboard android-jetpack-compose

我试图让我的 TextField 仅接受使用keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Text)以下字符:

      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),
                 .
                 .
                 .
Run Code Online (Sandbox Code Playgroud)

但软键盘仍然允许输入数字。如何让软键盘只允许输入字符?

Fra*_*esc 7

您需要过滤掉文本字段回调中的数字,

TextField(
            value = query3.value,
            onValueChange = { newValue ->
                query3.value = newValue.filter { !it.isDigit() }
            },
            singleLine = true,
Run Code Online (Sandbox Code Playgroud)