抽出來了一個方法來選擇時間(這里自己規定的只能選擇當前時間以后的日期),日期選擇完畢就會自動彈出時間選擇器讓選擇時間。
  /**   * 選擇日期和時間   */  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);  }以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答