예제 #1
0
 private void updateDaySpinner() {
   Calendar cal = Calendar.getInstance();
   // if year was not set, use 2000 as it was a leap year
   cal.set(mHasYear ? mYear : 2000, mMonth, 1);
   int max = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
   mDayPicker.setRange(1, max);
   mDayPicker.setCurrent(mDay);
 }
예제 #2
0
  public DatePicker(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    LayoutInflater inflater =
        (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.date_picker, this, true);

    mDayPicker = (NumberPicker) findViewById(R.id.day);
    mDayPicker.setFormatter(NumberPicker.TWO_DIGIT_FORMATTER);
    mDayPicker.setOnChangeListener(
        new OnChangedListener() {
          public void onChanged(NumberPicker picker, int oldVal, int newVal) {
            mDay = newVal;
            notifyDateChanged();
          }
        });
    mMonthPicker = (NumberPicker) findViewById(R.id.month);
    mMonthPicker.setFormatter(NumberPicker.TWO_DIGIT_FORMATTER);
    DateFormatSymbols dfs = new DateFormatSymbols();
    final String[] months = dfs.getShortMonths();

    /*
     * If the user is in a locale where the month names are numeric,
     * use just the number instead of the "month" character for
     * consistency with the other fields.
     */
    if (months[0].startsWith("1")) {
      for (int i = 0; i < months.length; i++) {
        months[i] = String.valueOf(i + 1);
      }
      mMonthPicker.setRange(1, 12);
    } else {
      mMonthPicker.setRange(1, 12, months);
    }

    mMonthPicker.setSpeed(200);
    mMonthPicker.setOnChangeListener(
        new OnChangedListener() {
          public void onChanged(NumberPicker picker, int oldVal, int newVal) {

            /* We display the month 1-12 but store it 0-11 so always
             * subtract by one to ensure our internal state is always 0-11
             */
            mMonth = newVal - 1;
            // Adjust max day of the month
            adjustMaxDay();
            notifyDateChanged();
            updateDaySpinner();
          }
        });
    mYearPicker = (NumberPicker) findViewById(R.id.year);
    mYearPicker.setSpeed(100);
    mYearPicker.setOnChangeListener(
        new OnChangedListener() {
          public void onChanged(NumberPicker picker, int oldVal, int newVal) {
            mYear = newVal;
            // Adjust max day for leap years if needed
            adjustMaxDay();
            notifyDateChanged();
            updateDaySpinner();
          }
        });

    mYearToggle = (CheckBox) findViewById(R.id.yearToggle);
    mYearToggle.setOnCheckedChangeListener(
        new OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            mHasYear = isChecked;
            adjustMaxDay();
            notifyDateChanged();
            updateSpinners();
          }
        });

    // attributes
    TypedArray a =
        context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.DatePicker);

    int mStartYear =
        a.getInt(com.android.internal.R.styleable.DatePicker_startYear, DEFAULT_START_YEAR);
    int mEndYear = a.getInt(com.android.internal.R.styleable.DatePicker_endYear, DEFAULT_END_YEAR);
    mYearPicker.setRange(mStartYear, mEndYear);

    a.recycle();

    // initialize to current date
    Calendar cal = Calendar.getInstance();
    init(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), null);

    // re-order the number pickers to match the current date format
    reorderPickers(months);

    if (!isEnabled()) {
      setEnabled(false);
    }
  }