Phonegap+html5+jquerymobile Developing Android-date controls

Source: Internet
Author: User

Original: Phonegap+html5+jquerymobile developing Android-date controls

Source code: Http://www.zuidaima.com/share/1550463778802688.htm

Date control under Android is not normal to call Android keyboard, need to use PhoneGap to the Android native control to call, but still not achieve the best results, find a temporary solution, to share with you, if you have a better solution everyone can share, One drawback of this approach is that the time control cannot be called when the control has a value.

/** * */package com.phonegap.plugins;import java.util.calendar;import java.util.date;import Org.json.JSONArray; Import Org.json.jsonexception;import Org.json.jsonobject;import Android.app.datepickerdialog;import Android.app.datepickerdialog.ondatesetlistener;import Android.app.timepickerdialog;import Android.app.timepickerdialog.ontimesetlistener;import Android.content.context;import Android.util.Log;import Android.widget.datepicker;import Android.widget.timepicker;import Org.apache.cordova.api.callbackcontext;import org.apache.cordova.api.cordovaplugin;/** * @author ng4e www.zuidaima.com * @author Daniel van ' t oever * * Rewrot E plugin so it similar to the IOS DatePicker plugin and it * accepts prefilled dates and time */public class Da Tepickerplugin extends Cordovaplugin {private static final string action_date = "DATE";p rivate static final string Action_    Time = "Time";p rivate final String pluginname = "Datepickerplugin"; @Overridepublic Boolean execute (String aCtion, Jsonarray args, Callbackcontext callbackcontext) {log.d (Pluginname, "DatePicker called with options:" + args); Retu RN this.show (args, callbackcontext);} Public Synchronized Boolean show (Final Jsonarray data, final Callbackcontext callbackcontext) {Final calendar c = Calendar . getinstance (); final Runnable runnable;final Context currentctx = cordova.getactivity (); final Datepickerplugin Datepickerplugin = this; String action = "date";/* Parse information from data parameter and where possible, override * above date fields */int m Onth =-1, day =-1, year =-1, hour =-1, min = -1;try {Jsonobject obj = data.getjsonobject (0); action = obj.getstring ("Mo De "); String optiondate = obj.getstring ("date"); string[] DatePart = Optiondate.split ("/"); month = Integer.parseint (datepart[0]);d ay = integer.parseint (datepart[1]); Year = Integer.parseint (datepart[2]); hour = Integer.parseint (datepart[3]); min = Integer.parseint (datepart[4]);/* Currently not handled in Android *///boolean optionallowolddates = Obj.getboolean ("Allowolddates");} catch (Jsonexception e) {e.printstacktrace ();} By default Initalize these-"now" final int myear = year = =-1? C.get (calendar.year): year;final int mmonth = Month = =-1? C.get (calendar.month): month-1;final int mday = Day = =-1? C.get (calendar.day_of_month): day;final int mhour = hour = 1? C.get (calendar.hour_of_day): hour;final int mminutes = min = =-1? C.get (Calendar.minute): Min;if (Action_time.equalsignorecase (ACTION)) {runnable = new runnable () {public void run () {fin Al Timesetlistener Timesetlistener = new Timesetlistener (Datepickerplugin, Callbackcontext); final TimePickerDialog Timedialog = new Timepickerdialog (Currentctx, Timesetlistener, Mhour,mminutes, True); Timedialog.show ();}};} else if (Action_date.equalsignorecase (ACTION)) {runnable = new runnable () {public void run () {final Datesetlistener Datese Tlistener = new Datesetlistener (Datepickerplugin, Callbackcontext); final Datepickerdialog datedialog = new DatePickeRdialog (Currentctx, Datesetlistener, Myear,mmonth, Mday);d atedialog.show ();}};} else {log.d (pluginname, "Unknown action. Only ' date ' or ' time ' is valid actions "); return false;} Cordova.getactivity (). Runonuithread (runnable); return true;} Private Final class Datesetlistener implements Ondatesetlistener {private final datepickerplugin datepickerplugin; Private final Callbackcontext callbackcontext;private Datesetlistener (Datepickerplugin datepickerplugin, Callbackcontext callbackcontext) {this.datepickerplugin = Datepickerplugin;this.callbackcontext = CallbackContext;} /** * Return A string containing the date in the format yyyy/mm/dd */public void Ondateset (Final DatePicker view, final in T year, Final int. monthofyear, final int dayofmonth) {String returndate = year + "/" + (Monthofyear + 1) + "/" + Dayofmont H;callbackcontext.success (returndate);}} Private Final class Timesetlistener implements Ontimesetlistener {private final datepickerplugin datepickerplugin; Private Final Callbackcontext CALlbackcontext;private Timesetlistener (Datepickerplugin datepickerplugin, Callbackcontext CallbackContext) { This.datepickerplugin = Datepickerplugin;this.callbackcontext = Callbackcontext;} /** * Return The current date with the time modified as it is set in the * Time picker. */public void Ontimeset (Final timepicker view, final int hourofday, final int minute) {Date date = new Date ();d Ate.sethour S (hourofday);d ate.setminutes (minute); Callbackcontext.success (date.tolocalestring ());}}}


Phonegap+html5+jquerymobile Developing Android-date controls

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.