Exemplo n.º 1
0
 public NumberPicker setupSpinner(int id, int min, int max) {
   NumberPicker mSpinner = (NumberPicker) findViewById(id);
   mSpinner.setMinValue(min);
   mSpinner.setMaxValue(max);
   mSpinner.setOnValueChangedListener(mOnChangeListener);
   mSpinner.setOnLongPressUpdateInterval(100);
   return mSpinner;
 }
Exemplo n.º 2
0
  public LunarDatePicker(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    // initialization based on locale
    setCurrentLocale(Locale.getDefault());

    // /@M:{comment these lines
    //         TypedArray attributesArray = context.obtainStyledAttributes(attrs,
    //         R.styleable.LunarDatePicker,
    //         defStyle, 0);
    //         boolean spinnersShown =
    //         attributesArray.getBoolean(R.styleable.LunarDatePicker_spinnersShown,
    //         DEFAULT_SPINNERS_SHOWN);
    //         boolean calendarViewShown = attributesArray.getBoolean(
    //         R.styleable.LunarDatePicker_calendarViewShown,
    //         DEFAULT_CALENDAR_VIEW_SHOWN);
    //         int startYear =
    //         attributesArray.getInt(R.styleable.LunarDatePicker_startYear,
    //         DEFAULT_START_YEAR);
    //         int endYear = attributesArray.getInt(R.styleable.LunarDatePicker_endYear,
    //         DEFAULT_END_YEAR);
    //         String minDate =
    //         attributesArray.getString(R.styleable.LunarDatePicker_minDate);
    //         String maxDate =
    //         attributesArray.getString(R.styleable.LunarDatePicker_maxDate);
    //         int layoutResourceId =R.layout.date_picker;
    //         attributesArray.getResourceId(R.styleable.LunarDatePicker_layout,
    //         R.layout.date_picker);
    //         attributesArray.recycle();
    // /}

    // /M:add @{
    boolean spinnersShown = DEFAULT_SPINNERS_SHOWN;
    boolean calendarViewShown = DEFAULT_CALENDAR_VIEW_SHOWN;
    int startYear = DEFAULT_START_YEAR;
    int endYear = DEFAULT_END_YEAR;
    int layoutResourceId = R.layout.date_picker;
    // /}

    LayoutInflater inflater =
        (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(layoutResourceId, this, true);

    OnValueChangeListener onChangeListener =
        new OnValueChangeListener() {
          public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            updateInputState();
            mTempDate.setTimeInMillis(mCurrentDate.getTimeInMillis());
            int gregorianYear = mTempDate.get(Calendar.YEAR);
            int gregorianMonth = mTempDate.get(Calendar.MONTH) + 1;
            int gregorianDay = mTempDate.get(Calendar.DAY_OF_MONTH);
            int lunarDates[] =
                LunarUtil.calculateLunarByGregorian(gregorianYear, gregorianMonth, gregorianDay);

            // take care of wrapping of days and months to update greater
            // fields
            if (picker == mDaySpinner) {
              if (oldVal > 27 && newVal == 1) {
                mTempDate.add(Calendar.DAY_OF_MONTH, 1);
              } else if (oldVal == 1 && newVal > 27) {
                mTempDate.add(Calendar.DAY_OF_MONTH, -1);
              } else {
                mTempDate.add(Calendar.DAY_OF_MONTH, newVal - oldVal);
              }
            } else if (picker == mMonthSpinner) {
              int leapMonth = 0;
              int monthCountDays = 0;
              if (oldVal > 10 && newVal == 0) {
                leapMonth = LunarUtil.leapMonth(lunarDates[0]);
                if (leapMonth == 12) {
                  monthCountDays = LunarUtil.daysOfLeapMonthInLunarYear(lunarDates[0]);
                } else {
                  monthCountDays = LunarUtil.daysOfALunarMonth(lunarDates[0], 12);
                }
                mTempDate.add(Calendar.DAY_OF_MONTH, monthCountDays);

              } else if (oldVal == 0 && newVal > 10) {
                leapMonth = LunarUtil.leapMonth(lunarDates[0] - 1);
                if (leapMonth == 12) {
                  monthCountDays = LunarUtil.daysOfLeapMonthInLunarYear(lunarDates[0]);
                } else {
                  monthCountDays = LunarUtil.daysOfALunarMonth(lunarDates[0] - 1, 12);
                }

                mTempDate.add(Calendar.DAY_OF_MONTH, -monthCountDays);
              } else {
                leapMonth = LunarUtil.leapMonth(lunarDates[0]);
                // move to previous
                if ((newVal - oldVal) < 0) {
                  if (leapMonth == 0) {
                    monthCountDays = LunarUtil.daysOfALunarMonth(lunarDates[0], newVal + 1);
                  } else { // leap year
                    if (newVal < leapMonth) {
                      monthCountDays = LunarUtil.daysOfALunarMonth(lunarDates[0], newVal + 1);
                    } else if (newVal == leapMonth) {
                      monthCountDays = LunarUtil.daysOfLeapMonthInLunarYear(lunarDates[0]);
                    } else {
                      monthCountDays = LunarUtil.daysOfALunarMonth(lunarDates[0], newVal);
                    }
                  }
                  monthCountDays = -monthCountDays;
                } else { // move to next month
                  if (leapMonth == 0) {
                    monthCountDays = LunarUtil.daysOfALunarMonth(lunarDates[0], oldVal + 1);
                  } else { // leap year
                    if (oldVal < leapMonth) {
                      monthCountDays = LunarUtil.daysOfALunarMonth(lunarDates[0], oldVal + 1);
                    } else if (oldVal == leapMonth) {
                      monthCountDays = LunarUtil.daysOfLeapMonthInLunarYear(lunarDates[0]);
                    } else {
                      monthCountDays = LunarUtil.daysOfALunarMonth(lunarDates[0], oldVal);
                    }
                  }
                }
                mTempDate.add(Calendar.DAY_OF_MONTH, monthCountDays);
              }
            } else if (picker == mYearSpinner) {
              int orientation =
                  newVal - oldVal > 0
                      ? LunarUtil.INCREASE_A_LUANR_YEAR
                      : LunarUtil.DECREATE_A_LUANR_YEAR;
              mTempDate =
                  LunarUtil.decreaseOrIncreaseALunarYear(
                      mTempDate, lunarDates[1], lunarDates[2], orientation);
            } else {
              throw new IllegalArgumentException();
            }
            // now set the date to the adjusted one
            setDate(
                mTempDate.get(Calendar.YEAR),
                mTempDate.get(Calendar.MONTH),
                mTempDate.get(Calendar.DAY_OF_MONTH));
            updateSpinners();
            updateCalendarView();
            notifyDateChanged();
          }
        };

    mSpinners = (LinearLayout) findViewById(R.id.pickers);

    // calendar view day-picker
    mCalendarView = (CalendarView) findViewById(R.id.calendar_view);
    mCalendarView.setOnDateChangeListener(
        new CalendarView.OnDateChangeListener() {
          public void onSelectedDayChange(CalendarView view, int year, int month, int monthDay) {
            setDate(year, month, monthDay);
            updateSpinners();
            notifyDateChanged();
          }
        });

    // day
    mDaySpinner = (NumberPicker) findViewById(R.id.day);
    // /M: comment this line @{
    // mDaySpinner.setFormatter(NumberPicker.TWO_DIGIT_FORMATTER);
    // /}
    mDaySpinner.setOnLongPressUpdateInterval(100);
    mDaySpinner.setOnValueChangedListener(onChangeListener);
    // mDaySpinner has 3 child,topButton,editText,bottomButtom
    if (mDaySpinner.getChildCount() == PICKER_CHILD_COUNT) {
      // set the Middle EditText of Numberpicker read only.
      mDaySpinnerInput = (EditText) mDaySpinner.getChildAt(1);
      mDaySpinnerInput.setClickable(false);
      mDaySpinnerInput.setFocusable(false);
    } else {
      // Normally,this will always not happen.
      mDaySpinnerInput = new EditText(context);
      Log.e(TAG, "mDaySpinner.getChildCount() != 3,It isn't init ok.");
    }

    // month
    mMonthSpinner = (NumberPicker) findViewById(R.id.month);
    mMonthSpinner.setMinValue(0);
    mMonthSpinner.setMaxValue(mNumberOfMonths - 1);
    mMonthSpinner.setDisplayedValues(mShortMonths);
    mMonthSpinner.setOnLongPressUpdateInterval(200);
    mMonthSpinner.setOnValueChangedListener(onChangeListener);
    // mDaySpinner has 3 child,topButton,editText,bottomButtom
    if (mMonthSpinner.getChildCount() == PICKER_CHILD_COUNT) {
      // set the Middle EditText of Numberpicker read only.
      mMonthSpinnerInput = (EditText) mMonthSpinner.getChildAt(1);
      mMonthSpinnerInput.setClickable(false);
      mMonthSpinnerInput.setFocusable(false);
    } else {
      // Normally,this will always not happen.
      mMonthSpinnerInput = new EditText(context);
      Log.e(TAG, "mMonthSpinner.getChildCount() != 3,It isn't init ok.");
    }
    // year
    mYearSpinner = (NumberPicker) findViewById(R.id.year);
    mYearSpinner.setOnLongPressUpdateInterval(100);
    mYearSpinner.setOnValueChangedListener(onChangeListener);
    // mDaySpinner has 3 child,topButton,editText,bottomButtom
    if (mYearSpinner.getChildCount() == PICKER_CHILD_COUNT) {
      // set the Middle EditText of Numberpicker read only.
      mYearSpinnerInput = (EditText) mYearSpinner.getChildAt(1);
      mYearSpinnerInput.setClickable(false);
      mYearSpinnerInput.setFocusable(false);
    } else {
      // Normally,this will always not happen.
      mYearSpinnerInput = new EditText(context);
      Log.e(TAG, "mYearSpinner.getChildCount() != 3,It isn't init ok.");
    }

    // show only what the user required but make sure we
    // show something and the spinners have higher priority
    if (!spinnersShown && !calendarViewShown) {
      setSpinnersShown(true);
    } else {
      setSpinnersShown(spinnersShown);
      setCalendarViewShown(calendarViewShown);
    }

    // set the min date giving priority of the minDate over startYear
    mTempDate.clear();
    mTempDate.set(startYear, 0, 1);
    setMinDate(mTempDate.getTimeInMillis());

    // set the max date giving priority of the maxDate over endYear
    mTempDate.clear();
    mTempDate.set(endYear, 11, 31);
    setMaxDate(mTempDate.getTimeInMillis());

    // initialize to current date
    mCurrentDate.setTimeInMillis(System.currentTimeMillis());
    init(
        mCurrentDate.get(Calendar.YEAR),
        mCurrentDate.get(Calendar.MONTH),
        mCurrentDate.get(Calendar.DAY_OF_MONTH),
        null);

    // re-order the number spinners to match the current date format
    reorderSpinners();

    // set content descriptions
    AccessibilityManager accessibilityManager =
        (AccessibilityManager) context.getSystemService(context.ACCESSIBILITY_SERVICE);
    if (accessibilityManager.isEnabled()) {
      setContentDescriptions();
    }
  }