如何在Android移动网站中强制使用数字键盘

发布于 2024-09-12 00:28:10 字数 583 浏览 6 评论 0原文

我有一个移动网站,其中有一些 HTML input 元素,如下所示:

<input type="text" name="txtAccessoryCost" size="6" />

我已将该网站嵌入到 WebView 用于可能的 Android 2.1 消耗,因此它也将是一个 Android 应用程序。

当此 HTML input 元素获得焦点时,是否可以使用数字键盘而不是默认的字母键盘?

或者,是否可以为整个应用程序设置它(也许是 清单文件),如果它对于 HTML 元素不可行?

I have a mobile website and it has some HTML input elements in it, like this:

<input type="text" name="txtAccessoryCost" size="6" />

I have embedded the site into a WebView for possible Android 2.1 consumption, so that it will also be an Android application.

Is it possible to get the keyboard with numbers instead of the default one with letters when this HTML input element is focused?

Or, is it possible to set it for the whole application (maybe something in the Manifest file), if it is not feasible for an HTML element?

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

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

发布评论

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

评论(8

万劫不复 2024-09-19 00:28:10
<input type="number" />
<input type="tel" />

当输入获得焦点时,这两个都会显示数字键盘。

<输入类型=“搜索”/>
显示带有额外搜索按钮的普通键盘

其他所有内容似乎都会显示标准键盘。

<input type="number" />
<input type="tel" />

Both of these present the numeric keypad when the input gains focus.

<input type="search" />
shows a normal keyboard with an extra search button

Everything else seems to bring up the standard keyboard.

ˇ宁静的妩媚 2024-09-19 00:28:10

重要说明

我将其发布为答案,而不是评论,因为它是相当重要的信息,并且以这种格式会吸引更多关注。

正如其他人指出的那样,您可以强制设备向您显示带有 type="number" / type="tel" 的数字键盘,但我必须强调 你必须对此非常谨慎。

如果有人期望一个以零开头的数字,例如 000222,那么她可能会遇到麻烦,因为某些浏览器(桌面 Chrome、实例)将发送到服务器222,这不是她想要的。

关于 type="tel" 我不能说任何类似的话,但我个人不使用它,因为它在不同电话上的行为可能会有所不同。我将自己限制在简单的 pattern="[0-9]*" 中,它在 Android 中不起作用

IMPORTANT NOTE

I am posting this as an answer, not a comment, as it is rather important info and it will attract more attention in this format.

As other fellows pointed, you can force a device to show you a numeric keyboard with type="number" / type="tel", but I must emphasize that you have to be extremely cautious with this.

If someone expects a number beginning with zeros, such as 000222, then she is likely to have trouble, as some browsers (desktop Chrome, for instance) will send to the server 222, which is not what she wants.

About type="tel" I can't say anything similar but personally I do not use it, as its behavior on different telephones can vary. I have confined myself to the simple pattern="[0-9]*" which do not work in Android

红焚 2024-09-19 00:28:10

inputmode 根据 WHATWG 规范 是默认方法。

对于 iOS 设备,添加 pattern 也可能有所帮助。

为了向后兼容,也请使用 type,因为 Chrome 从版本 66 开始使用这些。

<input
  inputmode="numeric"
  pattern="[0-9]*"
  type="number"
/>

inputmode according to WHATWG spec is the the default method.

For iOS devices adding pattern could also help.

For backward compatibility use type as well since Chrome use these as of version 66.

<input
  inputmode="numeric"
  pattern="[0-9]*"
  type="number"
/>
简单气质女生网名 2024-09-19 00:28:10

这应该有效。但我在 Android 手机上也遇到同样的问题。

<input type="number" /> <input type="tel" />

我发现,如果我不包含 jquerymobile 框架,键盘会在两种类型的字段上正确显示。

但是如果您确实需要使用 jquerymobile,我还没有找到解决该问题的解决方案。

更新:
我发现,如果表单标签存储在数字键盘之外,

<div data-role="page"> 

则不会显示数字键盘。这一定是一个错误...

This should work. But I have same problems on an Android phone.

<input type="number" /> <input type="tel" />

I found out, that if I didn't include the jquerymobile-framework, the keypad showed correctly on the two types of fields.

But I havn't found a solution to solve that problem, if you really need to use jquerymobile.

UPDATE:
I found out, that if the form-tag is stored out of the

<div data-role="page"> 

The number keypad isn't shown. This must be a bug...

毁梦 2024-09-19 00:28:10

当输入类型为“数字”时,某些浏览器会忽略向服务器发送前导零。因此,我使用 jquery 和 html 的混合来加载数字键盘,并确保该值作为文本而不是数字发送:

$(document).ready(function(){
$(".numberonly").focus(function(){$(this).attr("type","number")});
$(".numberonly").blur(function(){$(this).attr("type","text")});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="numberonly">

Some browsers igoners sending leading zero to the server when the input type is "number". So I use a mixing of jquery and html to load a numeric keypad and also make sure that the value is sent as a text not as a number:

$(document).ready(function(){
$(".numberonly").focus(function(){$(this).attr("type","number")});
$(".numberonly").blur(function(){$(this).attr("type","text")});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="numberonly">

黑寡妇 2024-09-19 00:28:10

对于带有小数点的数字键盘

<input inputmode="decimal" type="number" />

在此处输入图像描述

For a Numeric Keyboard with DECIMAL POINT

<input inputmode="decimal" type="number" />

enter image description here

平定天下 2024-09-19 00:28:10

为数字输入添加步骤属性

<input type="number" step="0.01">

来源:http:// /blog.pamelafox.org/2012/05/triggering-numeric-keyboards-with-html5.html

Add a step attribute to the number input

<input type="number" step="0.01">

Source: http://blog.pamelafox.org/2012/05/triggering-numeric-keyboards-with-html5.html

我只土不豪 2024-09-19 00:28:10

input type = number

当您想要提供数字输入时,可以使用 HTML5 input type="number" 属性值。

<input type="number" name="n" />

这是 iPhone 4 上出现的键盘:

iPhone Screenshot of HTML5 input type number
Android 2.2 使用此键盘进行 type=number:

Android Screenshot of HTML5 input type number

input type = number

When you want to provide a number input, you can use the HTML5 input type="number" attribute value.

<input type="number" name="n" />

Here is the keyboard that comes up on iPhone 4:

iPhone Screenshot of HTML5 input type number
Android 2.2 uses this keyboard for type=number:

Android Screenshot of HTML5 input type number

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文