ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-07-25 10:24:02  阅读:197  来源: 互联网

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


我有一个简单的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();
    }

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

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

解决方法:

在代码中进行以下更改

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.希望对你有帮助.. :)

标签:android-datepicker,android,datepicker,dialog,android-dialog
来源: https://codeday.me/bug/20190725/1532085.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有