自定义 HTML 页面中的软键盘类型

发布于 2024-11-16 01:08:36 字数 173 浏览 5 评论 0原文

有什么方法可以指定当用户点击 HTML 页面中的文本输入字段时显示哪种软键盘布局(我只需要数字)?

我可以更改 HTML 页面和 Javacode(我有一个嵌入 WebView 的活动)。但是,如果我更改 HTML 代码,它一定不会影响其他平台(另一个浏览器/设备仍应正确解释输入字段并显示软键盘,不一定是数字键盘)

Is there any way to specify which soft keyboard layout (I need only numbers) is shown when the user taps on a text input field in an HTML page?

I can change the HTML page and the Javacode (I have an activity embedding a WebView). However, it must not affect other platforms if I change the HTML code (another browser/device should still interpret the input field properly and show a soft keyboard, not necessarly a numbers only keyboard)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

滿滿的愛 2024-11-23 01:08:36

我没有尝试这样做,但理论上它应该有效:

使用 标记的 HTML5 type 属性将字段声明为仅接受数字。这将提示浏览器显示数字键盘。要使其仅在 Android 设备上生效,请考虑发出一些 Android 特定的 Javascript(它可以通过用户代理在客户端上进行平台检测,或者仅在检测到 Android 设备时才由服务器发送 - 再次)使用用户代理头),它遍历所有输入元素(可能用特殊类标记)并将type设置为number

I did not try this, but in theory it should work:

Use the HTML5 type attribute for the <input> tag to declare the field as accepting only numbers. This will give the browser the hint to display a numeric keyboard. To have this only take effect on Android devices, consider emitting some Android-specific Javascript (which could either do the platform detection on the client through the user agent or which could alternatively only be sent by the server if that detects and Android device - again using the user agent header), which traverses through all input elements (maybe marked with a special class) and sets the type to number.

绝不放开 2024-11-23 01:08:36

它们在不同的设备和操作系统版本中的工作方式似乎有所不同。它基本上取决于设备上运行的 WebKit 版本。如果它支持这些标签,它将起作用,否则,它会忽略该标签。

在 2.3.4 上,在 My Nexus S 上,输入 = 数字,给出数字键盘。

They seem to work differently in different devices and OS versions. It basically depends on the WebKit version that runs on a device. If it supports those tags, it will work, else, it ignores that tag.

On 2.3.4, on My Nexus S, type = number, gives a numeric keypad.

一百个冬季 2024-11-23 01:08:36

我还看到将“pattern=”[0-9]*“”添加到您的输入标签中。尝试:

<input type="number" size="4" maxlength="4" pattern="[0-9]*" ></input>

I've also seen that adding ' pattern="[0-9]*" ' to your input tag. Try:

<input type="number" size="4" maxlength="4" pattern="[0-9]*" ></input>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文