Qml TextField 数字键盘

Qml TextField numeric keyboard

我正在尝试使用仅接收 QML 中的数字的 TextField。我正在使用 inputMethodHints 属性 让设备键盘只显示数字。它在 Android 中工作正常,但当我 运行 在 iOS 中它显示完整的键盘,包括数字、字符和预测词。

代码如下:

TextField {
    id: numeroTelefoneTextField

    anchors.verticalCenter: parent.verticalCenter
    anchors.right: parent.right

    width: parent.width * 0.70
    height: parent.height * 0.6

    placeholderText: qsTr("Seu número")

    font.bold: true

    validator: RegExpValidator{regExp: /\d+/}

    inputMethodHints: Qt.ImhDigitsOnly
}

我尝试了其他选项来喜欢 inputMethodHints: Qt.ImhDigitsOnly | Qt.ImhNoPredictiveText 并且只是 inputMethodHints: Qt.ImhNoPredictiveText 但是 none 这个选项在 iOS 中有效。

我解决了这个问题,只删除了验证器,但我不知道为什么它在没有验证器的情况下工作。

代码如下:

TextField {
    id: numeroTelefoneTextField

    anchors.verticalCenter: parent.verticalCenter
    anchors.right: parent.right

    width: parent.width * 0.70
    height: parent.height * 0.6

    placeholderText: qsTr("Seu número")

    font.bold: true

    inputMethodHints: Qt.ImhDigitsOnly
}

尝试以下操作:

TextField {
    id: numeroTelefoneTextField

    anchors.verticalCenter: parent.verticalCenter
    anchors.right: parent.right

    width: parent.width * 0.70
    height: parent.height * 0.6

    placeholderText: qsTr("Seu número")

    validator: IntValidator {
        bottom: 1
        top: 100
    }

    font.bold: true

    inputMethodHints: Qt.ImhDigitsOnly
}