【问题标题】:Date/Time Pickers not giving changed values日期/时间选择器不提供更改的值
【发布时间】:2015-10-03 11:19:27
【问题描述】:

我创建了一个 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();

        }
    });

【问题讨论】:

    标签: java android datepicker timepicker


    【解决方案1】:

    发生这种情况是因为您将 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-22
      • 1970-01-01
      相关资源
      最近更新 更多