简体   繁体   English

从iOS / Android上的web-app调用本机日期选择器

[英]Invoke native date picker from web-app on iOS/Android

I'm trying to explore the posibilities with running a native web-app on different platforms using HTML5. 我正在尝试使用HTML5在不同平台上运行本机Web应用程序来探索可行性。 Currently, an <input type="date"> field just opens the standard soft keyboard on Android and iOS. 目前, <input type="date">字段只会在Android和iOS上打开标准软键盘。 I suppose that in the future the mobile OS's soft keyboards will include date pickers and such - just as <select> invokes the native select today. 我想在未来移动操作系统的软键盘将包括日期选择器等 - 正如<select>今天调用本机选择一样。

Since this isn't implemented on either Android or iOS, but is implemented in native UI, is it possible for a web-app to invoke the native date picker, ie when clicked on? 由于这不是在Android或iOS上实现,而是在本机UI中实现,因此Web应用程序是否可以调用本机日期选择器,即单击时?

This would make it possible for us to stop using JavaScript libraries like jQuery mobile and YUI. 这将使我们可以停止使用jQuery mobile和YUI等JavaScript库。

If my question is in any way unclear, please tell me. 如果我的问题不清楚,请告诉我。 Thank you in advance :-) 先感谢您 :-)

Since some years some devices support <input type="date"> but others don't, so one needs to be careful. 有些年头,有些设备支持<input type="date">但有些设备不支持,因此需要注意。 Here are some observations from 2012, which still might be valid today: 以下是2012年的一些观察结果,今天仍然有效:

  • One can detect if type="date" is supported by setting that attribute and then reading back its value. 通过设置该属性然后读回其值,可以检测是否支持type="date" Browsers/devices that don't support it will ignore setting the type to date and return text when reading back that attribute. 不支持它的浏览器/设备将忽略将类型设置为date并在读回该属性时返回text Alternatively, Modernizr can be used for detection. 或者, Modernizr可用于检测。 Beware that it's not enough to check for some Android version; 请注意,检查某些Android版本是不够的; like the Samsung Galaxy S2 on Android 4.0.3 does support type="date" , but the Google/Samsung Nexus S on the more recent Android 4.0.4 does not . 比如Android 4.0.3上的三星Galaxy S2确实支持type="date" ,但最近的Android 4.0.4上的Google / Samsung Nexus S却没有

  • When presetting the date for the native date picker, be sure to use a format the device recognizes. 在预设本机日期选择器的日期时,请务必使用设备可识别的格式。 When not doing that, devices might silently reject it, leaving one with an empty input field when trying to show an existing value. 如果不这样做,设备可能会静默拒绝它,在尝试显示现有值时会留下一个空输入字段。 Like using the date picker on a Galaxy S2 running Android 4.0.3 might itself set the <input> to 2012-6-1 for June 1st. 就像在运行Android 4.0.3的Galaxy S2上使用日期选择器一样,它本身可以将<input>设置为6月1日的2012-6-1 However, when setting the value from JavaScript, it needs leading zeroes: 2012-06-01 . 但是,从JavaScript设置值时,它需要前导零: 2012-06-01

  • When using things like Cordova (PhoneGap) to display the native date picker on devices that don't support type="date" : 使用Cordova(PhoneGap)之类的东西在不支持type="date"设备上显示本机日期选择器时:

    • Be sure to properly detect built-in support. 务必正确检测内置支持。 Like in 2012 on the Galaxy S2 running Android 4.0.3, erroneously also using the Cordova Android plugin would result in showing the date picker twice in a row: once again after clicking "set" in its first occurrence. 就像在2012年银河S2运行的是Android 4.0.3, 错误地使用科尔多瓦Android插件会导致显示的日期选择器连续两次:一次点击后,在其第一次出现的“设置”。

    • When there's multiple inputs on the same page, some devices show "previous" and "next" to get into another form field. 当同一页面上有多个输入时,某些设备会显示“previous”和“next”进入另一个表单字段。 On iOS 4, this does not trigger the onclick handler and hence gives the user a regular input. 在iOS 4上,这不会触发onclick处理程序,因此会为用户提供常规输入。 Using onfocus to trigger the plugin seemed to work better. 使用onfocus来触发插件似乎工作得更好。

    • On iOS 4, using onclick or onfocus to trigger the 2012 iOS plugin first made the regular keyboard show, after which the date picker was placed on top of that. 在iOS 4上,使用onclickonfocus触发2012 iOS插件首先进行常规键盘演示,然后将日期选择器置于其上。 Next, after using the date picker, one still needed to close the regular keyboard. 接下来,在使用日期选择器之后,仍然需要关闭常规键盘。 Using $(this).blur() to remove focus before the date picker was shown helped for iOS 4 and did not affect other devices I tested. 在显示日期选择器之前使用$(this).blur()删除焦点对iOS 4有帮助,并且不影响我测试的其他设备。 But it introduced some quick flashing of the keyboard on iOS, and things could be even more confusing on first usage as then the date picker was slower. 但是它在iOS上引入了一些快速闪存的键盘,在第一次使用时可能会更加混乱,因为日期选择器速度较慢。 One could fully disable the regular keyboard by making the input readonly if one were using the plugin, but that disabled the "previous" and "next" buttons when typing in other inputs on the same screen. 人们可以通过将输入完全禁用普通键盘readonly如果使用的插件,但禁用“前”,并在同一屏幕上的其他输入打字时“下一步”按钮。 It also seems the iOS 4 plugin did not make the native date picker show "cancel" or "clear". 似乎iOS 4插件没有使原生日期选择器显示“取消”或“清除”。

    • On an iOS 4 iPad (simulator), in 2012 the Cordova plugin did not seem to render correctly, basically not giving the user any option to enter or change a date. 在iOS 4 iPad(模拟器)上,2012年Cordova插件似乎无法正确呈现,基本上没有给用户任何输入或更改日期的选项。 (Maybe iOS 4 doesn't render its native date picker nicely on top of a web view, or maybe my web view's CSS styling has some effect, and surely this might be different on a real device: please comment or edit!) (也许iOS 4不能在Web视图上很好地呈现其原生日期选择器,或者我的Web视图的CSS样式可能会产生一些影响,当然这在真实设备上可能会有所不同:请评论或编辑!)

    • Though, again in 2012, the Android date picker plugin tried to use the same JavaScript API as the iOS plugin, and its example used allowOldDates , the Android version actually did not support that. 虽然,在2012年,Android日期选择器插件尝试使用与iOS插件相同的JavaScript API,并且其示例使用了allowOldDates ,但Android版本实际上并不支持。 Also, it returned the new date as 2012/7/2 while the iOS version returned Mon Jul 02 2012 00:00:00 GMT+0200 (CEST) . 此外,它返回新日期为2012/7/2而iOS版本返回Mon Jul 02 2012 00:00:00 GMT+0200 (CEST)

  • Even when <input type="date"> is supported, things might look messy: 即使支持<input type="date"> ,事情也可能看起来很乱:

    • iOS 5 nicely displays 2012-06-01 in a localized format, like 1 Jun. 2012 or June 1, 2012 (and even updates that immediately while still operating the date picker). iOS 5很好地以1 Jun. 2012 June 1, 2012 1 Jun. 20121 Jun. 2012 June 1, 2012的本地化格式显示2012-06-01 (甚至在仍然操作日期选择器时立即更新)。 However, the Galaxy S2 running Android 4.0.3 shows the ugly 2012-6-1 or 2012-06-01 , no matter which locale is used. 然而,运行Android 4.0.3的Galaxy S2显示了丑陋的2012-6-12012-06-01 ,无论使用哪种语言环境。

    • iOS 5 on an iPad (simulator) does not hide the keyboard when that is already visible when touching the date input, or when using "previous" or "next" in another input. 当触摸日期输​​入时,或者在另一个输入中使用“上一个”或“下一个”时,iPad(模拟器)上的iOS 5不会隐藏键盘。 It then simultaneously shows the date picker below the input and the keyboard at the bottom, and seems to allow any input from both. 然后它同时显示输入下方的日期选择器底部的键盘,并且似乎允许来自两者的任何输入。 However, though it does change the visible value, the keyboard input is actually ignored. 但是,虽然它确实更改了可见值,但实际上忽略了键盘输入。 (Shown when reading back the value, or when invoking the date picker again.) When the keyboard was not yet shown, then touching the date input only shows the date picker, not the keyboard. (在回读值时或在再次调用日期选择器时显示。)当键盘尚未显示时,触摸日期输​​入仅显示日期选择器,而不是键盘。 (This might be different on a real device, please comment or edit!) (这在真实设备上可能有所不同,请评论或编辑!)

    • Devices might show a cursor in the input field, and a long press might trigger the clipboard options, possibly showing the regular keyboard too. 设备可能会在输入字段中显示光标,长按可能会触发剪贴板选项,也可能显示常规键盘。 When clicking, some devices might even show the regular keyboard for a split second, before changing to show the date picker. 点击时,某些设备甚至可能会在一瞬间显示常规键盘,然后再更改为显示日期选择器。

iOS 5 now supports HTML5 better. iOS 5现在更好地支持HTML5。 in your webapp do 在你的webapp做

<input type="date" name="date" />

Android as of 4.0 lacks this type of native menu support. Android 4.0版本缺少此类本机菜单支持。

iOS5 has support for this ( Reference ). iOS5支持此功能( 参考 )。 If you want to invoke the native date picker you might have a an option with PhoneGap (have not tested this myself). 如果你想调用本机日期选择器,你可能有一个PhoneGap选项(我自己没有测试过)。

Give Mobiscroll a try. 试试Mobiscroll The scroller style date and time picker was especially created for interaction on touch devices. 滚动条样式的日期和时间选择器专门用于触摸设备上的交互。 It is pretty flexible, and easily customizable. 它非常灵活,可轻松定制。 It comes with iOS/Android themes. 它带有iOS / Android主题。

My answer is over-simplistic. 我的回答过于简单了。 If you like to write simple code that works cross-platform, then use the window.prompt method to ask the user for a date. 如果您想编写跨平台工作的简单代码,请使用window.prompt方法向用户询问日期。 Obviously you need to validate with a regex and then create the date object. 显然,您需要使用正则表达式进行验证,然后创建日期对象。

function onInputClick(e){
var r = window.prompt("Give me a date (YYYY-MM-DD)", "2014-01-01");
if(/[\d]{4}-[\d]{1,2}-[\d]{1,2}/.test(r)){
    //date ok
    e.value=r;
    var split=e.value.split("-");
    var date=new Date(parseInt(split[0]),parseInt(split[1])-1,parseInt(split[2]));
}else{
    alert("Invalid date. Try again.");
}
}

In you HTML: 在你的HTML中:

<input type="text" onclick="onInputClick(this)" value="2014-01-01">

You could use Trigger.io's UI module to use the native Android date / time picker with a regular HTML5 input. 您可以使用Trigger.io的UI模块将常规HTML日期/时间选择器与常规HTML5输入一起使用。 Doing that does require using the overall framework though (so won't work as a regular mobile web page). 这样做确实需要使用整体框架(因此不能用作常规移动网页)。

You can see before and after screenshots in this blog post: date time picker 您可以在此博客文章中查看屏幕截图之前和之后: 日期时间选择器

In HTML: 在HTML中:

  <form id="my_form"><input id="my_field" type="date" /></form>

In JavaScript 在JavaScript中

  // test and transform if needed if($('#my_field').attr('type') === 'text'){ $('#my_field').attr('type', 'text').attr('placeholder','aaaa-mm-dd'); }; // check if($('#my_form')[0].elements[0].value.search(/(19[0-9][0-9]|20[0-1][0-5])[- \\-.](0[1-9]|1[012])[- \\-.](0[1-9]|[12][0-9]|3[01])$/i) === 0){ $('#my_field').removeClass('bad'); } else { $('#my_field').addClass('bad'); }; 

On your form elements use input type="time" . 在表单元素上使用input type="time" It will save you all the hassle of trying to use a data picker library. 它将为您节省尝试使用数据选择器库的所有麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM