本文介绍了DatePickerDialog OK按钮没有得到选定的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a simple DatePickerDialog that opens when an EditText is opened. After choosing a date and pressing OK, it should be displayed at the same EditText. It works fine if I only use the default dialog where it creates only one button - OK. I added a Cancel button, the problem is that it only gets the current date.

下面是我的code:

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

如果我删除行 birthday.setText(selectedYear +(selectedMonth + 1)+ selectedDay); 下确定或BUTTON_POSITIVE,它工作正常。但是,在某些设备上,它不设置选定的日期到的EditText ,因为它只是呼吁在 datePickerListener 。所以,我决定加入行 birthday.setText(selectedYear +(selectedMonth + 1)+ selectedDay);在确定或BUTTON_POSITIVE但现在的问题是,它只能获取当前日期

If I delete the line birthday.setText(selectedYear + (selectedMonth + 1) + selectedDay); under OK or BUTTON_POSITIVE, it works fine. But on some devices, it doesn't set the selected date to the EditText since it was only called inside datePickerListener. So I decided adding the line birthday.setText(selectedYear + (selectedMonth + 1) + selectedDay); under OK or BUTTON_POSITIVE but the problem now is that it only gets the current date.

我对这个有点困惑。如果有人可能只是帮助我。

I'm a little confused by this. If someone could just help me.

推荐答案

请在code以下更改

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

要上班此code,你应该你的minSdkVersion更改为清单ATLEAST 11。希望这将帮助你..:)

To work this code, you should change your minSdkVersion to atleast 11 in Manifest. Hope this will help you.. :)

这篇关于DatePickerDialog OK按钮没有得到选定的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 22:51