单击“设置”按钮时,停止关闭DatePickerDialog [英] Stopping the DatePickerDialog from closing when use clicks the Set button

查看:132
本文介绍了单击“设置”按钮时,停止关闭DatePickerDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用DatePickerDialog .html rel = nofollow>此处。

I've implemented a DatePickerDialog using the example shown here.

在实现 DatePickerDialog.OnDateSetListener 的过程中,我添加了验证逻辑来​​检查所选日期是否在特定范围内

In my implementation of the DatePickerDialog.OnDateSetListener I added validation logic to check that the selected date is within specific range.

private final DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int y, int m,
            int d) {

            final Calendar calendar = Calendar.getInstance();
            calendar.set(y, m, d);
            Date date = calendar.getTime();

      if(!myValidationFunction(date)) {
        // date not within allowed range
        // cancel closing of dialog ?
      }
    }
};

我遇到的问题是 DatePickerDialog 当用户单击设置按钮时,如果验证规则失败,我想保持 DatePickerDialog 处于打开状态。

The problem I have is that the DatePickerDialog is closed automaticlly when the user clicks the set button and I want to keep the DatePickerDialog open if the validate rule fails.

有人知道当用户单击设置按钮时如何停止关闭 DatePickerDialog 吗?

Does anyone know how to stop the DatePickerDialog from closing when the user clicks the Set button?

推荐答案

DatePicker可从API 11验证您的日期。

From API 11 the DatePicker can validate your date for you.

遵循指南您所引用的,当覆盖onCreateDialog时,获取DatePicker并设置最小和最大日期:

Following the guide you refer to, when overriding onCreateDialog, get DatePicker and set min and max date:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // no changes from guide ...
    final DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
    dialog.getDatePicker().setMinDate(minDate);
    dialog.getDatePicker().setMaxDate(minDate);
    return dialog;
}

通过这种方式,用户无法选择错误的日期,因此无需手动验证

This way the user cannot pick a wrong date, thus no need to manually validate the date.

对于较早的版本,可以在允许关闭时使用布尔值进行控制,并实现自己的逻辑。
在这里,我尝试说明您需要在哪里扩展代码:

For older versions you can use a boolean for control when closing is allowed, and implement your own logic. Here I try to illustrate where you'll need to extend your code:

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day){

            @Override
            public void onBackPressed() {
                allowClose = true;
                super.onBackPressed();
            }

            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (which==DialogInterface.BUTTON_POSITIVE && validate()){
                    allowClose = true;
                }
                super.onClick(dialog, which);
            }

            @Override
            public void dismiss() {
                if (allowClose) {
                    super.dismiss();
                }
            }

        };

        return dialog;
    }

    private void onCancelBtnClick() {
        allowClose = true;
        dismiss();
    }

这篇关于单击“设置”按钮时,停止关闭DatePickerDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆