Android日期和时间选择器 Android日期和时间选择器实现代码
Glydi 人气:0想了解Android日期和时间选择器实现代码的相关内容吗,Glydi在本文为您仔细讲解Android日期和时间选择器的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android日期选择器,Android时间选择器,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); }
加载全部内容