Android 应用系统的日期和时间选择器

一、日期选择期

private void showDatePicker() {
        Calendar calendar = Calendar.getInstance();
        DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), R.style.MyDatePickerDialogTheme,
                dateSetListener,
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.show();
    }


private DatePickerDialog.OnDateSetListener dateSetListener =
            (view, year, month, dayOfMonth) -> {
                yourView.setText(year + "/" + (month + 1) + "/" + dayOfMonth);
            };

二、时间选择器

private void showTimePicker() {
        Calendar calendar = Calendar.getInstance();
        TimePickerDialog timePickerDialog = new TimePickerDialog(getActivity(), R.style.MyDatePickerDialogTheme,
                timeSetListener,
                calendar.get(Calendar.HOUR_OF_DAY),
                calendar.get(Calendar.MINUTE), true);
        timePickerDialog.show();
    }


private TimePickerDialog.OnTimeSetListener timeSetListener = (view, hourOfDay, minute) -> {
        String minStr = minute < 10 ? ("0" + minute) : minute + "";
        yourView.setText(hourOfDay + ":" + minStr);
    };

三、遇到选择的取消和确认可以点击但是不显示的问题

由于某些原因按钮的字体颜色有时显示为白色

为了解决这个问题,我向 TimePickerDialog 添加了一个对话框主题来设置按钮文本颜色。

这是我添加到 styles.xml 的 xml 代码

关于android - TimePickerDialog 取消和确定按钮未显示,我们在Stack Overflow上找到一个类似的问题: android - TimePickerDialog CANCEL and OK button not showing - Stack Overflow

你可能感兴趣的:(布局控件,android)