仅限制jetpack compose中TextField中的数字

问题描述 投票:0回答:5

我只想在文本字段中输入数字。我尝试了这个stackoverflow逻辑来限制字母和特殊字符,但是当我按键盘上的点时,它崩溃了。

错误

2022-08-18 09:47:13.966 8050-8050/com.abc.app.dev E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.abc.app.dev, PID: 8050
    java.lang.NumberFormatException: For input string: "."
        at java.lang.Integer.parseInt(Integer.java:733)
        at java.lang.Integer.parseInt(Integer.java:865)
        at com.abc.app.yo.composable.InputKt$InputWithUnitContainer$1$1$1$1.invoke(Input.kt:187)
        at com.abc.app.yo.composable.InputKt$InputWithUnitContainer$1$1$1$1.invoke(Input.kt:186)
        at androidx.compose.foundation.text.BasicTextFieldKt$BasicTextField$7$1.invoke(BasicTextField.kt:266)
        at androidx.compose.foundation.text.BasicTextFieldKt$BasicTextField$7$1.invoke(BasicTextField.kt:264)
        at androidx.compose.foundation.text.CoreTextFieldKt$CoreTextField$onValueChangeWrapper$1.invoke(CoreTextField.kt:241)
        at androidx.compose.foundation.text.CoreTextFieldKt$CoreTextField$onValueChangeWrapper$1.invoke(CoreTextField.kt:236)
        at androidx.compose.foundation.text.TextFieldDelegate$Companion.onEditCommand(TextFieldDelegate.kt:198)
        at androidx.compose.foundation.text.TextFieldDelegate$Companion.access$onEditCommand(TextFieldDelegate.kt:90)
        at androidx.compose.foundation.text.TextFieldDelegate$Companion$restartInput$1.invoke(TextFieldDelegate.kt:246)
        at androidx.compose.foundation.text.TextFieldDelegate$Companion$restartInput$1.invoke(TextFieldDelegate.kt:243)
        at androidx.compose.ui.text.input.TextInputServiceAndroid$createInputConnection$1.onEditCommands(TextInputServiceAndroid.android.kt:111)
        at androidx.compose.ui.text.input.RecordingInputConnection.endBatchEditInternal(RecordingInputConnection.android.kt:162)
        at androidx.compose.ui.text.input.RecordingInputConnection.addEditCommandWithBatch(RecordingInputConnection.android.kt:136)
        at androidx.compose.ui.text.input.RecordingInputConnection.commitText(RecordingInputConnection.android.kt:181)
        at com.android.internal.inputmethod.RemoteInputConnectionImpl.lambda$commitText$16$com-android-internal-inputmethod-RemoteInputConnectionImpl(RemoteInputConnectionImpl.java:569)
        at com.android.internal.inputmethod.RemoteInputConnectionImpl$$ExternalSyntheticLambda34.run(Unknown Source:8)
        at android.os.Handler.handleCallback(Handler.java:942)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7898)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

代码

appendTextFieldValue: (TextFieldValue) -> Unit,

这是传递函数,下面是我的文本字段

        TextField(
                value = textFieldValue,
                singleLine = true,
                onValueChange = {
                    if (it.text.length <= maxLength && it.text.toInt() <= maxLength) {
                        appendTextFieldValue(it)
                    }
                    onIsErrorChange(false)
                },
                keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
                isError = isError,
            )
android kotlin android-jetpack-compose android-jetpack android-compose-textfield
5个回答
19
投票

您可以使用正则表达式模式。

类似:

val pattern = remember { Regex("^\\d+\$") }

TextField(
    value = text,
    onValueChange = {
        if (it.isEmpty() || it.matches(pattern)) {
            text = it
        }
    },
    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
)

3
投票

这就是我为实现这一目标所做的事情:

TextField(
    value = text,
    onValueChange = { if (it.isDigitsOnly()) text = it },
    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
)

2
投票

您可以过滤字符串,如`

TextField(
    value = text,
    onValueChange = {
        if (it.isEmpty()) {
            text = it.filter { symbol ->
                symbol.isDigit()
            }
        }
    },
    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
)

2
投票

如果您想仅显示数字键盘,只需执行以下操作:

TextField(
     value = textState,
     onValueChange = { text ->
         textState = text
     },
     keyboardOptions = KeyboardOptions.Default.copy(
         keyboardType = KeyboardType.NumberPassword
     ),
     visualTransformation = VisualTransformation.None
)

0
投票

您可以将文本字段限制为仅包含数字

var amount by remember { mutableStateOf("") }    
TextField(value = amount, onValueChange = {if (it.isDigitsOnly()) amount = it}
© www.soinside.com 2019 - 2024. All rights reserved.