日期/时间选择器未给出更改的值

时间:2015-07-14 17:09:56

标签: java android datepicker timepicker

我创建了一个AlertDialog,其中包含DatePicker和TimePicker。每次打开时,日期和时间选择器都会被初始化,无论日期是用于打开它的按钮的文本。当用户单击对话框中的“设置”按钮时,应该将按钮上的文本更新为所选的日期和时间,但它会将其保留在旧的日期和时间。

看来使用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();

        }
    });

1 个答案:

答案 0 :(得分:2)

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

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

您已膨胀的第一个视图包含您在适当时间设置的DatePickerTimePicker,而默认情况下,新视图设置为Calendar.getInstance()。< / p>

更改此行:

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

到这一行:

dateAndTimePickerDialog.setView(dialogView);