【问题标题】:How to set date on date picker based on other date picker in android如何根据android中的其他日期选择器在日期选择器上设置日期
【发布时间】:2013-12-17 10:17:22
【问题描述】:

我使用下面的代码来生成运行时日期选择器。

public class MultiDatePickerActivity extends Activity {

private TextView startDateDisplay;
private TextView endDateDisplay;
private Button startPickDate;
private Button endPickDate;
private Calendar startDate;
private Calendar endDate;

static final int DATE_DIALOG_ID = 0;

private TextView activeDateDisplay;
private Calendar activeDate;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.multidatepicker);

    /*  capture our View elements for the start date function   */
    startDateDisplay = (TextView) findViewById(R.id.startDateDisplay);
    startPickDate = (Button) findViewById(R.id.startPickDate);

    /* get the current date */
    startDate = Calendar.getInstance();

    /* add a click listener to the button   */
    startPickDate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDateDialog(startDateDisplay, startDate);
        }
    });

    /* capture our View elements for the end date function */
    endDateDisplay = (TextView) findViewById(R.id.endDateDisplay);
    endPickDate = (Button) findViewById(R.id.endPickDate);

    /* get the current date */
    endDate = Calendar.getInstance();

    /* add a click listener to the button   */
    endPickDate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDateDialog(endDateDisplay, endDate);
        }
    });

    /* display the current date (this method is below)  */
    updateDisplay(startDateDisplay, startDate);
    updateDisplay(endDateDisplay, endDate);
}

private void updateDisplay(TextView dateDisplay, Calendar date) {
    dateDisplay.setText(
            new StringBuilder()
                // Month is 0 based so add 1
                .append(date.get(Calendar.MONTH) + 1).append("-")
                .append(date.get(Calendar.DAY_OF_MONTH)).append("-")
                .append(date.get(Calendar.YEAR)).append(" "));

}

public void showDateDialog(TextView dateDisplay, Calendar date) {
    activeDateDisplay = dateDisplay;
    activeDate = date;
    showDialog(DATE_DIALOG_ID);
}

private OnDateSetListener dateSetListener = new OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        activeDate.set(Calendar.YEAR, year);
        activeDate.set(Calendar.MONTH, monthOfYear);
        activeDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        updateDisplay(activeDateDisplay, activeDate);
        unregisterDateDisplay();
    }
};

private void unregisterDateDisplay() {
    activeDateDisplay = null;
    activeDate = null;
}

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this, dateSetListener, activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
    }
    return null;
}

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
    super.onPrepareDialog(id, dialog);
    switch (id) {
        case DATE_DIALOG_ID:
            ((DatePickerDialog) dialog).updateDate(activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
            break;
    }
}
}

我希望当我按下 endDate 时它不应该显示当前日期,我希望它检查我在 startDate 中选择的日期,它应该在 endDate 选择器中显示下一个 startDate 日期。

这意味着我想根据 startDate(开始日期的下一个日期)设置 endDate。假设我今天是 12-05-2013 并且如果我选择 15-05-2013 作为开始日期,那么它应该在 endDate 选择器中显示 16-05-2013。那么我怎样才能做到这一点。

请帮助我。任何帮助,将不胜感激。提前致谢。

【问题讨论】:

    标签: android date android-datepicker


    【解决方案1】:

    日历有 add 方法。来自文档:

    将给定的金额添加到日历字段。

    要增加 1 天,您可以这样做:

    yourDestinationCalendar.add(Calendar.DATE, 1);
    

    yourDestinationCalendar 的初始化方式与您用来填充小部件的日历相同

    【讨论】:

      【解决方案2】:

      您可以在DatePickerDialog..中设置最小值和最大值。

      从第一个对话框 (startDate) 中获取日期并在当天添加一天,如下面的代码。

      Calendar mCalendarTo = Calendar.getInstance();
      mCalendarTo.set(year_value, month_value, day_value);
      mCalendarTo.add(Calendar.DATE, 1);
      

      将最小值设置为DatePickerDialog(结束日期)

      mDatePickerDialog.getDatePicker().setMinDate(mCalendarTo.getTimeInMillis());
      

      【讨论】:

      【解决方案3】:

      如果我正确理解您的问题,我认为您应该在 OnDateSetListener 中为您的开始日期选择器执行此操作。

      使用 blackbelt 建议的代码将一天添加到传递给 onDateSet 方法的日期,并在您的 endDatePicker 的 updateDisplay 方法中使用这个新的 Calendar 实例

      这是一个可以适应您的代码的快速示例

          Button startDateButton = (Button)findViewById(R.id.start_date_button);
          Button endDateButton = (Button)findViewById(R.id.end_date_button);
          Calendar cal = Calendar.getInstance();
      
          _endDateListener = new DatePickerDialog.OnDateSetListener() {
              @Override
              public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                  //Do what ever you need to do in the end date listener
              }
          };
      
          _startDateListener = new DatePickerDialog.OnDateSetListener() {
              @Override
              public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                  Calendar cal = Calendar.getInstance();
                  cal.set(year, monthOfYear, dayOfMonth);
                  cal.add(Calendar.DATE, 1);
                  _endDateDialog = new DatePickerDialog(_context, _endDateListener, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
              }
          };
      
      
          _startDateDialog = new DatePickerDialog(this, _startDateListener, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
          _endDateDialog =  new DatePickerDialog(this, _endDateListener, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
      
          startDateButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  _startDateDialog.show();
              }
          });
      
          endDateButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  _endDateDialog.show();
              }
          });
      

      希望这已经让你明白了

      【讨论】:

      • 您的开始日期和结束日期似乎使用了相同的日期选择器。您需要为每个单独使用一个。然后在OnDateSetListener为你的开始日期你可以设置结束日期DatePicker的日期。
      【解决方案4】:

      在这种情况下,结束日期将在用户选择的开始日期后 1 天。 希望这会有所帮助.. :)

      public class MainActivity extends AppCompatActivity {
          Button StartDate, EndDate;
          int year_x, month_x, day_x;
          int year_y, month_y, day_y;
          static final int DIALOG_ID_1 = 0;
          static final int DIALOG_ID_2 = 1;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
              setSupportActionBar(toolbar);
      
              final Calendar calendar = Calendar.getInstance();
              year_x = calendar.get(Calendar.YEAR);
              month_x = calendar.get(Calendar.MONTH);
              day_x = calendar.get(Calendar.DAY_OF_MONTH);
      
              year_y = year_x;
              month_y = month_x;
              day_y = day_x + 1;
      
              showDialogOnButtonClick();
      
          }
      
          public void showDialogOnButtonClick() {
              StartDate = (Button) findViewById(R.id.bt_startDate);
              StartDate.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      showDialog(DIALOG_ID_1);
                  }
              });
              EndDate = (Button) findViewById(R.id.bt_EndDate);
              EndDate.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      showDialog(DIALOG_ID_2);
                  }
              });
          }
      
          @Override
          protected Dialog onCreateDialog(int id) {
              if (id == DIALOG_ID_1) {
                  return new DatePickerDialog(this, dpickerlistener, year_x, month_x, day_x);
              } else if (id == DIALOG_ID_2)
                  return new DatePickerDialog(this, dpickerlistener2, year_y, month_y, day_y);
              else return null;
      
          }
      
          private DatePickerDialog.OnDateSetListener dpickerlistener = new DatePickerDialog.OnDateSetListener() {
              @Override
              public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                  year_x = year;
                  month_x = monthOfYear + 1;
                  day_x = dayOfMonth;
                  Toast.makeText(MainActivity.this, "Start Date : " + day_x + " / " + month_x + " / " + year_x, Toast.LENGTH_SHORT).show();
              }
          };
      
      
          private DatePickerDialog.OnDateSetListener dpickerlistener2 = new DatePickerDialog.OnDateSetListener() {
              @Override
              public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
      
                  year_y = year;
                  month_y = monthOfYear + 1;
                  day_y = dayOfMonth;
                  Toast.makeText(MainActivity.this, "End Date : " + day_y + " / " + month_y + " / " + year_y, Toast.LENGTH_SHORT).show();
              }
          };
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多