简体   繁体   English

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

[英]DatePickerDialog OK button not getting the selected date

I have a simple DatePickerDialog that opens when an EditText is opened. 我有一个简单的DatePickerDialog ,打开EditText打开。 After choosing a date and pressing OK, it should be displayed at the same EditText . 选择日期并按OK后,它应显示在同一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. 我添加了一个取消按钮,问题是它只获取当前日期。

Here's my 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();
    }

If I delete the line birthday.setText(selectedYear + (selectedMonth + 1) + selectedDay); 如果我删除了birthday.setText(selectedYear + (selectedMonth + 1) + selectedDay);birthday.setText(selectedYear + (selectedMonth + 1) + selectedDay); under OK or BUTTON_POSITIVE, it works fine. 在OK或BUTTON_POSITIVE下,它工作正常。 But on some devices, it doesn't set the selected date to the EditText since it was only called inside datePickerListener . 但是在某些设备上,它没有将所选日期设置为EditText因为它只在datePickerListener So I decided adding the line birthday.setText(selectedYear + (selectedMonth + 1) + selectedDay); 所以我决定添加一行birthday.setText(selectedYear + (selectedMonth + 1) + selectedDay); under OK or BUTTON_POSITIVE but the problem now is that it only gets the current date. 在OK或BUTTON_POSITIVE下但现在的问题是它只获取当前日期。

I'm a little confused by this. 我有点困惑。 If someone could just help me. 如果有人可以帮助我。

Make the following changes in your 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();

To work this code, you should change your minSdkVersion to atleast 11 in Manifest. 要使用此代码,您应该在Manifest中将minSdkVersion更改为至少11。 Hope this will help you.. :) 希望这个能对您有所帮助.. :)

Try calling datePickerDialog.getDatePicker().clearFocus() in the beginning of onClick method: 尝试在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);
                }
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在DatePickerDialog中的onDateChanged上隐藏“确定”按钮 - Hide Ok button on onDateChanged in DatePickerDialog 如何控制datePickerDialog上的“确定”按钮? (Android) - How to control the “ok” button on datePickerDialog? (Android) Android:在DatePickerDialog中获取上一个日期 - Android: Getting previous date in DatePickerDialog 未使用在Datepicker对话框中选择的日期字符串更新EditText - EditText not updated with Date string selected in Datepickerdialog 如何将datepicker对话框中的选定日期传递给Cursor - how to pass selected Date in datepickerdialog in to Cursor Android:datepickerdialog取消对话框而未获取日期 - Android: datepickerdialog cancel dialog without getting the date 如何将以前选择的日期放入Android DatePickerDialog中? - How do I put previously selected date in an Android DatePickerDialog? 将选定的日期从DatePickerDialog复制到主页的Edittext框中 - Copy selected date from DatePickerDialog to Edittext box in main page 如何在下次调用时在DatePickerDialog中显示以前选择的日期? - How do I show the previously selected date in a DatePickerDialog on next call? 单击“中性”按钮时,在DatePickerDialog中设置日期 - Get Date set in DatePickerDialog when Neutral button is clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM