2013-01-03 16 views
3

我使用的下一個日期選擇器插件:日期選擇器插件PhoneGap的取消按鈕

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

我如何能識別,當用戶按下「取消」按鈕?:

控制器的圖像:http://goo.gl/YUzus

,我使用的代碼是:

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

由於

回答

1

我管理的取消事件修改位的.java插件: 代碼之後

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

我添加一個事件來捕獲取消:

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);}}}); 

這將設置日期按「取消」按鈕時「」。 在JavaScript代碼中,我設法改變焦點事件,挖掘事件如下(我使用的插件在移動具有TAP事件:

$('.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(); 
        } 
      }); 
     }); 

我希望能幫助

+0

我不得不添加進口的android .content.DialogInterface;到文件頂部以及 – Nathan

+0

並使用callbackContext.success(「」);而不是datePickerPlugin.success – Nathan

相關問題