Ejemplo n.º 1
0
  public void setTime(int year, int month, int day) {
    if (month > 12) return;
    if (day > 31) return;
    if (month <= 0 || year <= 0 || day <= 0) return;

    // 设置数据
    yearPicker.setData(calendarUtil.getYears());
    monthPicker.setData(calendarUtil.getMonths());
    dayPicker.setData(calendarUtil.getDays(year, month - 1));
    // 设置默认值为当前
    yearPicker.setDefault(year - 1970);
    monthPicker.setDefault(month - 1);
    if ((day - 1) >= calendarUtil.getDays(year, month - 1).size()) {
      dayPicker.setDefault(calendarUtil.getDays(year, month - 1).size() - 1);
    } else {
      dayPicker.setDefault(day - 1);
    }
  }
Ejemplo n.º 2
0
  @Override
  protected void onFinishInflate() {
    // TODO Auto-generated method stub
    super.onFinishInflate();
    LayoutInflater.from(getContext()).inflate(R.layout.su_date_picker, this);
    // 初始化日历
    calendar = Calendar.getInstance();
    calendarUtil = CalendarUtil.getSingleton();
    // 获取控件引用
    yearPicker = (ScrollerNumberPicker) findViewById(R.id.year);
    monthPicker = (ScrollerNumberPicker) findViewById(R.id.month);
    dayPicker = (ScrollerNumberPicker) findViewById(R.id.day);
    // 获取当前年月日
    int y = calendar.get(Calendar.YEAR);
    int m = calendar.get(Calendar.MONTH);
    int d = calendar.get(Calendar.DAY_OF_MONTH);
    // 设置数据
    yearPicker.setData(calendarUtil.getYears());
    monthPicker.setData(calendarUtil.getMonths());
    dayPicker.setData(calendarUtil.getDays(y, m));
    // 设置默认值为当前
    yearPicker.setDefault(y - 1970);
    monthPicker.setDefault(m);
    dayPicker.setDefault(d - 1);

    yearPicker.setOnSelectListener(
        new OnSelectListener() {

          @Override
          public void endSelect(int id, String text) {
            // TODO Auto-generated method stub
            if (text == null || text.equals("")) return;
            if (tempYearIndex != id) {
              String selectDay = dayPicker.getSelectedText();
              if (selectDay == null || selectDay.equals("")) return;
              String selectMonth = monthPicker.getSelectedText();
              if (selectMonth == null || selectMonth.equals("")) return;
              dayPicker.setData(
                  calendarUtil.getDays(Integer.valueOf(text), Integer.valueOf(selectMonth) - 1));
              int lastDay = Integer.valueOf(dayPicker.getItemText(dayPicker.getListSize() - 1));
              if (Integer.valueOf(selectDay) > lastDay) {
                dayPicker.setDefault(lastDay - 1);
              } else {
                dayPicker.setDefault(Integer.valueOf(selectDay) - 1);
              }
            }
            tempYearIndex = id;
            Message message = new Message();
            message.what = REFRESH_VIEW;
            handler.sendMessage(message);
          }

          @Override
          public void selecting(int id, String text) {
            // TODO Auto-generated method stub

          }
        });

    monthPicker.setOnSelectListener(
        new OnSelectListener() {

          @Override
          public void endSelect(int id, String text) {
            // TODO Auto-generated method stub
            if (text.equals("") || text == null) return;
            if (tempMonthIndex != id) {
              String selectDay = dayPicker.getSelectedText();
              if (selectDay == null || selectDay.equals("")) return;
              String selectYear = yearPicker.getSelectedText();
              if (selectYear == null || selectYear.equals("")) return;
              dayPicker.setData(
                  calendarUtil.getDays(Integer.valueOf(selectYear), Integer.valueOf(text) - 1));
              int lastDay = Integer.valueOf(dayPicker.getItemText(dayPicker.getListSize() - 1));
              if (Integer.valueOf(selectDay) > lastDay) {
                dayPicker.setDefault(lastDay - 1);
              } else {
                dayPicker.setDefault(Integer.valueOf(selectDay) - 1);
              }
            }
            tempMonthIndex = id;
            Message message = new Message();
            message.what = REFRESH_VIEW;
            handler.sendMessage(message);
          }

          @Override
          public void selecting(int id, String text) {
            // TODO Auto-generated method stub

          }
        });

    dayPicker.setOnSelectListener(
        new OnSelectListener() {

          @Override
          public void selecting(int id, String text) {
            // TODO Auto-generated method stub

          }

          @Override
          public void endSelect(int id, String text) {
            // TODO Auto-generated method stub
            Message message = new Message();
            message.what = REFRESH_VIEW;
            handler.sendMessage(message);
          }
        });
  }