Android Date Time picker in one dialog

后端 未结 9 1213
抹茶落季
抹茶落季 2021-02-07 01:31

I am using material Date Time picker for my Android app. But I want to combine the Date and Time picker in one dialog.

<
9条回答
  •  春和景丽
    2021-02-07 02:00

    Googled this problem and found usefull Enamul Haque's answer. Modified his code little bit and got this result. Its quick solution and will be improved, but works nice)

    public class DateTimePicker {
    
        @NonNull
        private final Calendar calendar = Calendar.getInstance();
        @Nullable
        private DatePickerDialog datePickerDialog;
        @Nullable
        private TimePickerDialog timePickerDialog;
        @Nullable
        private ResultCallback dateResultCallback;
    
        public void showDialog(@NonNull Context context, long time) {
            calendar.setTimeInMillis(time);
            closeDialogs();
            showDatePicker(context);
        }
    
        @Nullable
        public ResultCallback getDateResultCallback() {
            return dateResultCallback;
        }
    
        public void setDateResultCallback(@Nullable ResultCallback dateResultCallback) {
            this.dateResultCallback = dateResultCallback;
        }
    
        public long getTime() {
            return calendar.getTimeInMillis();
        }
    
        private void closeDialogs() {
            if (datePickerDialog != null) {
                datePickerDialog.dismiss();
                datePickerDialog = null;
            }
            if (timePickerDialog != null) {
                timePickerDialog.dismiss();
                timePickerDialog = null;
            }
        }
    
        private DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                calendar.set(Calendar.YEAR, year);
                calendar.set(Calendar.MONTH, month);
                calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                timePicker(view.getContext());
            }
        };
    
        private TimePickerDialog.OnTimeSetListener timeSetListener = new TimePickerDialog.OnTimeSetListener() {
    
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                calendar.set(Calendar.MINUTE, minute);
                if (dateResultCallback != null) {
                    dateResultCallback.onResult(calendar.getTime());
                }
            }
        };
    
        private void showDatePicker(@NonNull Context context) {
            datePickerDialog = new DatePickerDialog(context,
                    dateSetListener,
                    calendar.get(Calendar.YEAR),
                    calendar.get(Calendar.MONTH),
                    calendar.get(Calendar.DAY_OF_MONTH));
            datePickerDialog.show();
        }
    
        private void timePicker(@NonNull Context context) {
            timePickerDialog = new TimePickerDialog(context,
                    timeSetListener,
                    calendar.get(Calendar.HOUR_OF_DAY),
                    calendar.get(Calendar.MINUTE),
                    true);
            timePickerDialog.show();
        }
    
        public void release() {
            closeDialogs();
            dateResultCallback = null;
        }
    
    }
    

    Dialogs calls via method showDialog(...), possible to release object when it not need anymore (in onDestroy() in activity f.e). And again - thanks Enamul Haque for idea.

提交回复
热议问题