Пример #1
0
 /**
  * Sets the maximal date supported by this {@link DatePicker} in milliseconds since January 1,
  * 1970 00:00:00 in {@link TimeZone#getDefault()} time zone.
  *
  * @param maxDate The maximal supported date.
  */
 public void setMaxDate(long maxDate) {
   mTempDate.setTimeInMillis(maxDate);
   if (mTempDate.get(Calendar.YEAR) == mMaxDate.get(Calendar.YEAR)
       && mTempDate.get(Calendar.DAY_OF_YEAR) != mMaxDate.get(Calendar.DAY_OF_YEAR)) {
     return;
   }
   mMaxDate.setTimeInMillis(maxDate);
   mCalendarView.setMaxDate(maxDate);
   if (mCurrentDate.after(mMaxDate)) {
     mCurrentDate.setTimeInMillis(mMaxDate.getTimeInMillis());
     updateCalendarView();
   }
   updateSpinners();
 }
Пример #2
0
  public DateTimePicker(
      Context context,
      String dateFormat,
      String dateTimeValue,
      PickersState state,
      String minDateValue,
      String maxDateValue) {
    super(context);

    setCurrentLocale(Locale.getDefault());

    mState = state;
    LayoutInflater inflater = LayoutInflater.from(context);
    inflater.inflate(R.layout.datetime_picker, this, true);

    mOnChangeListener = new OnValueChangeListener();

    mDateSpinners = (LinearLayout) findViewById(R.id.date_spinners);
    mTimeSpinners = (LinearLayout) findViewById(R.id.time_spinners);
    mPickers = (LinearLayout) findViewById(R.id.datetime_picker);

    // We will display differently according to the screen size width.
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    DisplayMetrics dm = new DisplayMetrics();
    display.getMetrics(dm);
    mScreenWidth = display.getWidth() / dm.densityDpi;
    mScreenHeight = display.getHeight() / dm.densityDpi;

    if (DEBUG) {
      Log.d(LOGTAG, "screen width: " + mScreenWidth + " screen height: " + mScreenHeight);
    }

    // Set the min / max attribute.
    try {
      if (minDateValue != null && !minDateValue.equals("")) {
        mMinDate.setTime(new SimpleDateFormat(dateFormat).parse(minDateValue));
      } else {
        mMinDate.set(DEFAULT_START_YEAR, Calendar.JANUARY, 1);
      }
    } catch (Exception ex) {
      Log.e(LOGTAG, "Error parsing format sting: " + ex);
      mMinDate.set(DEFAULT_START_YEAR, Calendar.JANUARY, 1);
    }

    try {
      if (maxDateValue != null && !maxDateValue.equals("")) {
        mMaxDate.setTime(new SimpleDateFormat(dateFormat).parse(maxDateValue));
      } else {
        mMaxDate.set(DEFAULT_END_YEAR, Calendar.DECEMBER, 31);
      }
    } catch (Exception ex) {
      Log.e(LOGTAG, "Error parsing format string: " + ex);
      mMaxDate.set(DEFAULT_END_YEAR, Calendar.DECEMBER, 31);
    }

    // Find the initial date from the constructor arguments.
    try {
      if (!dateTimeValue.equals("")) {
        mTempDate.setTime(new SimpleDateFormat(dateFormat).parse(dateTimeValue));
      } else {
        mTempDate.setTimeInMillis(System.currentTimeMillis());
      }
    } catch (Exception ex) {
      Log.e(LOGTAG, "Error parsing format string: " + ex);
      mTempDate.setTimeInMillis(System.currentTimeMillis());
    }

    if (mMaxDate.before(mMinDate)) {
      // If the input date range is illogical/garbage, we should not restrict the input range (i.e.
      // allow the
      // user to select any date). If we try to make any assumptions based on the illogical min/max
      // date we could
      // potentially prevent the user from selecting dates that are in the developers intended
      // range, so it's best
      // to allow anything.
      mMinDate.set(DEFAULT_START_YEAR, Calendar.JANUARY, 1);
      mMaxDate.set(DEFAULT_END_YEAR, Calendar.DECEMBER, 31);
    }

    // mTempDate will either be a site-supplied value, or today's date otherwise. CalendarView
    // implementations can
    // crash if they're supplied an invalid date (i.e. a date not in the specified range), hence we
    // need to set
    // a sensible default date here.
    if (mTempDate.before(mMinDate) || mTempDate.after(mMaxDate)) {
      mTempDate.setTimeInMillis(mMinDate.getTimeInMillis());
    }

    // If we're displaying a date, the screen is wide enough
    // (and if we're using an SDK where the calendar view exists)
    // then display a calendar.
    if (Versions.feature11Plus
        && (mState == PickersState.DATE || mState == PickersState.DATETIME)
        && mScreenWidth >= SCREEN_SIZE_THRESHOLD) {

      if (DEBUG) {
        Log.d(LOGTAG, "SDK > 10 and screen wide enough, displaying calendar");
      }

      mCalendar = new CalendarView(context);
      mCalendar.setVisibility(GONE);

      LayoutParams layoutParams = new LayoutParams(250, 280);
      mCalendar.setLayoutParams(layoutParams);
      mCalendar.setFocusable(true);
      mCalendar.setFocusableInTouchMode(true);
      mCalendar.setMaxDate(mMaxDate.getTimeInMillis());
      mCalendar.setMinDate(mMinDate.getTimeInMillis());
      mCalendar.setDate(mTempDate.getTimeInMillis(), false, false);

      mCalendar.setOnDateChangeListener(
          new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(CalendarView view, int year, int month, int monthDay) {
              mTempDate.set(year, month, monthDay);
              setDate(mTempDate);
              notifyDateChanged();
            }
          });

      mPickers.addView(mCalendar);
    } else {
      // If the screen is more wide than high, we are displaying day and
      // time spinners, and if there is no calendar displayed, we should
      // display the fields in one row.
      if (mScreenWidth > mScreenHeight && mState == PickersState.DATETIME) {
        mPickers.setOrientation(LinearLayout.HORIZONTAL);
      }
      mCalendar = null;
    }

    // Initialize all spinners.
    mDaySpinner = setupSpinner(R.id.day, 1, mTempDate.get(Calendar.DAY_OF_MONTH));
    mDaySpinner.setFormatter(TWO_DIGIT_FORMATTER);
    mDaySpinnerInput = (EditText) mDaySpinner.getChildAt(1);

    mMonthSpinner =
        setupSpinner(R.id.month, 1, mTempDate.get(Calendar.MONTH) + 1); // Month is 0-based
    mMonthSpinner.setFormatter(TWO_DIGIT_FORMATTER);
    mMonthSpinner.setDisplayedValues(mShortMonths);
    mMonthSpinnerInput = (EditText) mMonthSpinner.getChildAt(1);

    mWeekSpinner = setupSpinner(R.id.week, 1, mTempDate.get(Calendar.WEEK_OF_YEAR));
    mWeekSpinner.setFormatter(TWO_DIGIT_FORMATTER);
    mWeekSpinnerInput = (EditText) mWeekSpinner.getChildAt(1);

    mYearSpinner = setupSpinner(R.id.year, DEFAULT_START_YEAR, DEFAULT_END_YEAR);
    mYearSpinnerInput = (EditText) mYearSpinner.getChildAt(1);

    mAMPMSpinner = setupSpinner(R.id.ampm, 0, 1);
    mAMPMSpinner.setFormatter(TWO_DIGIT_FORMATTER);

    if (mIs12HourMode) {
      mHourSpinner = setupSpinner(R.id.hour, 1, 12);
      mAMPMSpinnerInput = (EditText) mAMPMSpinner.getChildAt(1);
      mAMPMSpinner.setDisplayedValues(mShortAMPMs);
    } else {
      mHourSpinner = setupSpinner(R.id.hour, 0, 23);
      mAMPMSpinnerInput = null;
    }

    mHourSpinner.setFormatter(TWO_DIGIT_FORMATTER);
    mHourSpinnerInput = (EditText) mHourSpinner.getChildAt(1);

    mMinuteSpinner = setupSpinner(R.id.minute, 0, 59);
    mMinuteSpinner.setFormatter(TWO_DIGIT_FORMATTER);
    mMinuteSpinnerInput = (EditText) mMinuteSpinner.getChildAt(1);

    // The order in which the spinners are displayed are locale-dependent
    reorderDateSpinners();

    // Set the date to the initial date. Since this date can come from the user,
    // it can fire an exception (out-of-bound date)
    try {
      updateDate(mTempDate);
    } catch (Exception ex) {
    }

    // Display only the pickers needed for the current state.
    displayPickers();
  }