代码保持 运行 没有正确的顺序

code keeps running without proper sequence

在我的代码中,我有一个日期和时间选择器....但出于某种原因,代码不会等到我选择任何东西,一旦显示 mDateSetListener 对话框,Log.d 就会显示在 Logcat 今天的日期。 也是第一次显示对话框时,它没有显示 mTimeSetListener,我必须再次请求它才能正常工作 这是我的代码

static void dateAndTimePicker(final Context context, final ListView selectedFood, final int type) {

    contextGlobal = context;
    dateTime = new int[5];
    cal = Calendar.getInstance();
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH);
    int day = cal.get(Calendar.DAY_OF_MONTH);

    DatePickerDialog dialog = new DatePickerDialog(contextGlobal,
            android.R.style.Theme_Holo_Light_Dialog_MinWidth, mDateSetListener, year, month, day);
    Objects.requireNonNull(dialog.getWindow()).setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    dialog.show();

    mDateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker datePicker, int year, int month, int day) {
            month = month + 1; /*january is 0 (months start with 0 not 1)*/
            Log.d("date is", month + "/" + day + "/" + year);
            dateTime[0] = year;
            dateTime[1] = month;
            dateTime[2] = day;
            hour = cal.get(Calendar.HOUR_OF_DAY);
            minute = cal.get(Calendar.MINUTE);

            TimePickerDialog timePickerDialog = new TimePickerDialog(contextGlobal,
                    android.R.style.Theme_Holo_Light_Dialog_MinWidth, mTimeSetListener, hour, minute, false);
            timePickerDialog.show();
        }

    };

    mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker timePicker, int hour, int minute) {
            Log.d("Time is", hour + ":" + minute);
            dateTime[3] = hour;
            dateTime[4] = minute;
            addFood(contextGlobal, dateTime, selectedFood, type);

        }
    };

    Log.d("date", String.valueOf(day));

}

提前致谢

but for some reason, the code doesn't wait till I pick anything and once the mDateSetListener dialog is shown the Log.d shows in the Logcat today's date.

这是因为,onDateSet是某种异步调用(等待用户执行操作),只有当用户点击所需的日期表单时才会触发DatePickerDialog

also the first time the dialog is shown, it doesn't show mTimeSetListener, and I have to request it for a second time for it to work properly

-- -- --