You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kivy iOS应用如何让指定TextInput聚焦时唤起iOS数字软键盘

属性支持情况澄清

Kivy原生的input_type属性目前确实未对接iOS系统的软键盘类型配置,仅在Android、桌面端生效,因此你看到的"该属性不支持iOS软键盘"的说法是属实的。

实现指定输入框唤起iOS数字软键盘的方案

你需要通过kivy-ios默认自带的pyobjus桥接iOS原生API,监听数字输入框的聚焦事件动态修改键盘类型即可,具体步骤如下:

1. 自定义数字输入框类

在Python代码中定义继承自TextInput的子类,监听focus事件动态切换键盘类型:

from kivy.uix.textinput import TextInput
from pyobjus import autoclass

class NumberTextInput(TextInput):
    def on_focus(self, instance, value):
        UITextField = autoclass('UITextField')
        current_field = UITextField.appearance()
        if value:
            # 聚焦时设置为数字键盘,UIKeyboardTypeNumberPad对应枚举值为4
            current_field.setKeyboardType_(4)
        else:
            # 失焦时恢复默认键盘,不影响其他文本输入框
            current_field.setKeyboardType_(0)

2. 修改KV代码使用自定义组件

将你原本的数字输入框组件替换为上面定义的NumberTextInput即可,无需再设置input_type属性:

# 替换your_file_name为你存放NumberTextInput类的Python文件名
#:import NumberTextInput your_file_name.NumberTextInput

    NumberTextInput:
        id: eta1
        markup: True
        multiline: False
        font_size: '20sp'
        hint_text: "ETA 1 in UTC"
        opacity: 1 if self.text else .7
        padding_y: [self.height / 2.0 - (self.line_height / 2.0) * len(self._lines), 0]
        padding_x: [10, 0]
        text: self.text.upper() if self.text is not None else ''
        size_hint: .4, .08
        pos_hint: {"top": .775, "center_x": .71}

补充说明

如果需要支持带小数点的数字输入,将上面的键盘类型枚举值从4修改为5即可,对应iOS的小数键盘类型。

内容的提问来源于stack exchange,提问作者PW1990

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 14:09:04