自定义 HTML 页面中的软键盘类型
有什么方法可以指定当用户点击 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我没有尝试这样做,但理论上它应该有效:
使用
标记的 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 thetype
tonumber
.它们在不同的设备和操作系统版本中的工作方式似乎有所不同。它基本上取决于设备上运行的 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.
我还看到将“pattern=”[0-9]*“”添加到您的输入标签中。尝试:
I've also seen that adding ' pattern="[0-9]*" ' to your input tag. Try: