Android日期和时间选择器实现代码
更新时间:2017年11月10日 10:18:10 作者:Glydi
这篇文章主要为大家详细介绍了Android日期和时间选择器实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
抽出来了一个方法来选择时间(这里自己规定的只能选择当前时间以后的日期),日期选择完毕就会自动弹出时间选择器让选择时间。
/**
* 选择日期和时间
*/
private void selectDataAndTime() {
// 获取当前时间
final Calendar calendar = Calendar.getInstance();
/*
* toast("当前时间是:" + calendar.get(Calendar.YEAR) + "," +
* calendar.get(Calendar.MONTH) + "," +
* calendar.get(Calendar.DAY_OF_MONTH));
*/
// 日期选择对话框
dataPickerDialog = new DatePickerDialog(this, new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
// 判断用户选择的日期是否合法
if (calendar.get(Calendar.YEAR) > year) {
toast("时间有误,请从新选择");
return;
} else if (calendar.get(Calendar.YEAR) == year) {
if (calendar.get(Calendar.MONTH) > month) {
toast("时间有误,请从新选择");
return;
} else if (calendar.get(Calendar.MONTH) == month) {
if (calendar.get(Calendar.DAY_OF_MONTH) > day) {
toast("时间有误,请从新选择");
return;
} else {
strDate = year + "-" + (month + 1) + "-" + day;
if (timePickerDialog != null) {
timePickerDialog.show();
}
}
} else {
strDate = year + "-" + (month + 1) + "-" + day;
if (timePickerDialog != null) {
timePickerDialog.show();
}
}
} else {
strDate = year + "-" + (month + 1) + "-" + day;
if (timePickerDialog != null) {
timePickerDialog.show();
}
}
}
}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar
.get(Calendar.DAY_OF_MONTH));
// 时间选择对话框
timePickerDialog = new TimePickerDialog(this, new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hour, int minute) {
strTime = strDate + " " + hour + ":" + minute;
timeTt.setText(strTime);
}
}, calendar.get(Calendar.HOUR), calendar.get(Calendar.MINUTE), true);
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
解决Android Studio Design界面不显示layout控件的问题
这篇文章主要介绍了解决Android Studio Design界面不显示layout控件的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-03-03
Android开发中的Surface库及用其制作播放器UI的例子
这篇文章主要介绍了Android开发中的Surface库及用其制作播放器界面的例子,利用SurfaceView和SurfaceHolder可以高效地绘制和控制图形界面,需要的朋友可以参考下2016-04-04
开源电商app常用标签"hot"之第三方开源LabelView
这篇文章主要介绍了开源电商app常用标签"hot"之第三方开源LabelView,对开源电商app相关资料感兴趣的朋友一起学习吧2015-12-12


最新评论