抽出來了一個方法來選擇時間(這里自己規(guī)定的只能選擇當(dāng)前時間以后的日期),日期選擇完畢就會自動彈出時間選擇器讓選擇時間。
/** * 選擇日期和時間 */ private void selectDataAndTime() { // 獲取當(dāng)前時間 final Calendar calendar = Calendar.getInstance(); /* * toast("當(dāng)前時間是:" + 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); }以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選