DatePickerDialog确定按钮未获取所选日期

时间:2013-12-19 09:28:01

标签: android dialog datepicker android-dialog android-datepicker

我有一个简单的DatePickerDialog,在EditText打开时会打开。选择日期并按OK后,它应显示在同一EditText。如果我只使用默认对话框,它只创建一个按钮就可以正常工作 - 好的。我添加了一个取消按钮,问题是它只获取当前日期。

这是我的代码:

    private void showDatePicker(String birthdayStr) {
        // TODO Auto-generated method stub
        final Calendar c = Calendar.getInstance();

        if (birthdayStr.equals("")) {
            yearStr = c.get(Calendar.YEAR);
            monthStr = c.get(Calendar.MONTH);
            dayStr = c.get(Calendar.DAY_OF_MONTH);
        }

        DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
            // when dialog box is closed, below method will be called.
            public void onDateSet(DatePicker view, int selectedYear,
                    int selectedMonth, int selectedDay) {
                if (isOkayClicked) {
                    birthday.setText(selectedYear + (selectedMonth + 1) + selectedDay);
                    yearStr = selectedYear;
                    monthStr = selectedMonth;
                    dayStr = selectedDay;
                }
                isOkayClicked = false;
            }
        };
        DatePickerDialog datePickerDialog = new DatePickerDialog(
                RegistrationTwoActivity.this, datePickerListener, yearStr,
                monthStr, dayStr);

        datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
                getString(R.string.cancel),
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        if (which == DialogInterface.BUTTON_NEGATIVE) {
                            dialog.cancel();
                            isOkayClicked = false;
                        }
                    }
                });

        datePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        if (which == DialogInterface.BUTTON_POSITIVE) {
                            isOkayClicked = true;
                            birthday.setText(selectedYear + (selectedMonth + 1) + selectedDay);
                        }
                    }
                });
        datePickerDialog.setCancelable(false);
        datePickerDialog.show();
    }

如果我在OK或BUTTON_POSITIVE下删除行birthday.setText(selectedYear + (selectedMonth + 1) + selectedDay);,它可以正常工作。但在某些设备上,它并未将所选日期设置为EditText,因为它仅在datePickerListener内调用。所以我决定在OK或BUTTON_POSITIVE下添加行birthday.setText(selectedYear + (selectedMonth + 1) + selectedDay);但现在的问题是它只获取当前日期。

我有点困惑。如果有人可以帮助我。

2 个答案:

答案 0 :(得分:9)

在代码中进行以下更改

final DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
                    // when dialog box is closed, below method will be called.
                    public void onDateSet(DatePicker view, int selectedYear,
                            int selectedMonth, int selectedDay) {
                        if (isOkayClicked) {
                            birthday.setText(selectedYear + (selectedMonth + 1)
                                    + selectedDay);
                            yearStr = selectedYear;
                            monthStr = selectedMonth;
                            dayStr = selectedDay;
                        }
                        isOkayClicked = false;
                    }
                };
                final DatePickerDialog datePickerDialog = new DatePickerDialog(
                        RegistrationTwoActivity.this, datePickerListener,
                        yearStr, monthStr, dayStr);

                datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
                        getString(R.string.cancel),
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                if (which == DialogInterface.BUTTON_NEGATIVE) {
                                    dialog.cancel();
                                    isOkayClicked = false;
                                }
                            }
                        });

                datePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE,
                        "OK", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                if (which == DialogInterface.BUTTON_POSITIVE) {
                                    isOkayClicked = true;
                                    DatePicker datePicker = datePickerDialog
                                            .getDatePicker();
                                    datePickerListener.onDateSet(datePicker,
                                            datePicker.getYear(),
                                            datePicker.getMonth(),
                                            datePicker.getDayOfMonth());
                                }
                            }
                        });
                datePickerDialog.setCancelable(false);
                datePickerDialog.show();

要使用此代码,您应该在Manifest中将minSdkVersion更改为至少11。希望这会对你有所帮助.. :)

答案 1 :(得分:-1)

尝试在onClick方法的开头调用datePickerDialog.getDatePicker().clearFocus()

    final DatePickerDialog datePickerDialog = ...
    ...

    datePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    datePickerDialog.getDatePicker().clearFocus();
                    isOkayClicked = true;
                    birthday.setText(selectedYear + (selectedMonth + 1) + selectedDay);
                }
            });