Date/Time 选择器未提供更改的值

Date/Time Pickers not giving changed values

我创建了一个 AlertDialog,其中包含 DatePicker 和 TimePicker。每次打开日期和时间选择器时,都会将它们初始化为用于打开它的按钮文本上的任何日期。当用户单击对话框中的 "Set" 按钮时,它应该将按钮上的文本更新为选定的日期和时间,但它会保留旧的日期和时间。

似乎使用 datePicker.getYear() 和所有其他方法都没有在选取器中获取选定的数据,因为我做了一个 Toast 来测试它并且它只显示旧信息。

代码如下:

startDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //get date from button text
            Calendar timeInButton = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US);
            try {
                timeInButton.setTime(sdf.parse(startDate.getText().toString()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            int pYear = timeInButton.get(Calendar.YEAR);
            int pMonth = timeInButton.get(Calendar.MONTH);
            int pDay = timeInButton.get(Calendar.DAY_OF_MONTH);
            int pHour = timeInButton.get(Calendar.HOUR_OF_DAY);
            int pMinute = timeInButton.get(Calendar.MINUTE);

            final View dialogView = View.inflate(MyActivity.this, R.layout.date_picker_dialog, null);

            final DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.date_picker);
            datePicker.init(pYear, pMonth, pDay, null);

            final TimePicker timePicker = (TimePicker) dialogView.findViewById(R.id.time_picker);
            timePicker.setCurrentHour(pHour);
            timePicker.setCurrentMinute(pMinute);

            AlertDialog.Builder dateAndTimePickerDialog = new AlertDialog.Builder(AddCashGame.this);
            dateAndTimePickerDialog.setTitle("Select Starting Date and Time");

            LayoutInflater inflater = AddCashGame.this.getLayoutInflater();

            dateAndTimePickerDialog.setView(inflater.inflate(R.layout.date_picker_dialog, null));
            dateAndTimePickerDialog.setPositiveButton("Set", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    int newYear = datePicker.getYear();
                    int newMonth = datePicker.getMonth() + 1;
                    int newDay = datePicker.getDayOfMonth();
                    int newHour = timePicker.getCurrentHour() % 12;
                    String newMinute = formatter.format(timePicker.getCurrentMinute());
                    String newAMOrPM = timePicker.getCurrentHour() % 12 <= 12 ? " AM" : " PM";

                    String newTime = newMonth + "/" + newDay + "/" + newYear + "   " + newHour + ":" + newMinute + newAMOrPM;

                    startDate.setText(newTime);
                    Toast.makeText(AddCashGame.this, newTime + "",Toast.LENGTH_SHORT).show();
                }
            });
            dateAndTimePickerDialog.setNegativeButton("Cancel", null);
            dateAndTimePickerDialog.show();

        }
    });

发生这种情况是因为您将 AlertDialog View 指向一个新的,而不是您在此行中膨胀的那个:

final View dialogView = View.inflate(MyActivity.this, R.layout.date_picker_dialog, null);

您膨胀的第一个视图包含设置为正确时间的 DatePickerTimePicker,而新视图默认设置为 Calendar.getInstance()

更改此行:

dateAndTimePickerDialog.setView(inflater.inflate(R.layout.date_picker_dialog, null));

到这一行:

dateAndTimePickerDialog.setView(dialogView);