如何从BottomSheetDialogFragment创建TimePickerDIalog

时间:2018-06-14 14:55:18

标签: android timepicker

我正在使用DatepickerDialog& TimePickerDialog,我已经建立了一个Datepicker但我不能使DatePickerDialog工作。我可以像使用DatepickerDialog一样实现这个吗?

这是我的代码:

private void timePicker(){

int hour = 0;
int minute = 0;
TimePickerDialog timePickerDialog;
timePickerDialog = new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() {
    @Override
    public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute, int second) {
        Toast.makeText(getContext(), Integer.toString(hourOfDay), Toast.LENGTH_SHORT).show();
    }
}, hour, minute, false);

public void showDatePickerDialog() {
    final Calendar calendar = Calendar.getInstance();
    int yy = calendar.get(Calendar.YEAR);
    int mm = calendar.get(Calendar.MONTH);
    int dd = calendar.get(Calendar.DAY_OF_MONTH);
    DatePickerDialog datePicker = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            String date = String.valueOf(dayOfMonth) +"-"+String.valueOf(monthOfYear)
                    +"-"+String.valueOf(year);
            textViewDate.setText(date);
        }
    }, yy, mm, dd);
    datePicker.show();
}

enter image description here

1 个答案:

答案 0 :(得分:0)

仅用于测试目的,尝试使用较新的片段类,而不是兼容性类。它们彼此不兼容。