android - datepicker 插件 phonegap 取消按钮

标签 android jquery datepicker phonegap-plugins

我使用下一个 datePicker 插件:

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

我如何识别用户何时按下“取消”按钮?:

Controller 图像:http://goo.gl/YUzus

我使用的代码是:

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

谢谢

最佳答案

我通过稍微修改 .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();
                    }
            });
        });

希望能帮到你

关于android - datepicker 插件 phonegap 取消按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14148917/

相关文章:

java - 在 while 循环中比较两个相等的整数时,等于运算符失败?

javascript - jQuery 通过下拉选择中的几列过滤表

javascript - jQuery 根据标签标签从 span 标签返回文本

javascript - Ruby on Rails Javascript 在开发/生产方面有所不同

javascript - 在 bootstrap timepicker-angular js 中验证

android - 有没有函数可以计算两次之间的差异并显示相对结果?

android - 安装应用程序的顺序正在改变行为?

javascript - 在 #DIV 上调用函数而不是 window()

android - 如何从 Android 中的 DatePickerDialog 返回日期值?

java - 如何迭代 Android JavascriptInterface 返回的项目?