一、日期选择期
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 代码文章来源:https://www.toymoban.com/news/detail-723589.html
<style name="MyDatePickerDialogTheme" parent="android:Theme.Material.Light.Dialog">
<item name="android:colorAccent">@color/common_bg_color</item>
<item name="android:textColor">@color/common_bg_color</item>
<item name="colorOnPrimary">@color/common_bg_color</item>
</style>
关于android - TimePickerDialog 取消和确定按钮未显示,我们在Stack Overflow上找到一个类似的问题: android - TimePickerDialog CANCEL and OK button not showing - Stack Overflow文章来源地址https://www.toymoban.com/news/detail-723589.html
到了这里,关于Android 应用系统的日期和时间选择器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!