androidjquerydatepickerphonegap-plugins

datepicker plugin phonegap cancel button


I use the next datePicker plugin:

https://github.com/phonegap/phonegap-plugins/tree/master/Android/DatePicker

How can I recognize when a user press the "Cancel" button?:

The image of controller:

enter image description here

The code that I use is:

window.plugins.datePicker.show({                        
    date : new Date(date),
    mode : 'date',
    allowOldDates : true    
},function(selectedDate) { 
    console.log('SET button was pressed');      
});

Thanks


Solution

  • I managed the cancel event modifying a bit the .java plugin : after the code

    public void run() {
    final DateSetListener dateSetListener = new DateSetListener(datePickerPlugin, callBackId);
    final DatePickerDialog dateDialog = new DatePickerDialog(currentCtx, dateSetListener, mYear,mMonth, mDay);
    

    I added the event to trap the cancel :

    dateDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() 
    {
        public void onClick(DialogInterface dialog, int which) 
    {
        if (which == DialogInterface.BUTTON_NEGATIVE) {
        datePickerPlugin.success(new PluginResult(PluginResult.Status.NO_RESULT, ""), callBackId);}}});
    

    This will set the date to "" when pressed the cancel button. In the javascript code i managed to change the focus event to tap event as follow (I use the plugin on mobile that have TAP events:

     $('.nativedatepicker').bind('tap',function(event) {
                var currentField = $(this);
    
                console.log(currentField.val());
                var myNewDate = Date.parse(currentField.val()) || new Date();
    
                if(typeof myNewDate === "number"){ myNewDate = new Date (myNewDate); }
                //myNewDate = currentField.val();
    
                console.log("inside native date picker " + event);
    
                // Same handling for iPhone and Android
                window.plugins.datePicker.show({
                    date : myNewDate,
                    mode : 'date', // date or time or blank for both
                    allowOldDates : true
                }, function(returnDate) {
                    console.log("ret date" + returnDate);
                    if (returnDate)
                        {
                            var newDate = new Date(returnDate);
                            currentField.val(returnDate);
    
                            // This fixes the problem you mention at the bottom of this script with it not working a second/third time around, because it is in focus.
                            currentField.blur();
                        }
                });
            });
    

    I hope can help.