大佬教程收集整理的这篇文章主要介绍了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日期和时间选择器实现代码全部内容,希望文章能够帮你解决Android日期和时间选择器实现代码所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。