如何根据android中的其他日期选择器在日期选择器上设置日期

时间:2013-12-17 10:17:23

标签: android date android-datepicker

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

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。那么我怎样才能做到这一点。

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

4 个答案:

答案 0 :(得分:0)

Calendar具有add方法。来自文档:

  

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

要添加1天,您可以执行以下操作:

yourDestinationCalendar.add(Calendar.DATE, 1);

其中yourDestinationCalendar的初始化方式与用于填充小部件的日历的方式相同

答案 1 :(得分:0)

您可以在DatePickerDialog ..

中设置最小值和最大值

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

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

将最小值设置为DatePickerDialog(endDate)

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

答案 2 :(得分:0)

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

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

以下是一个可以适应代码的简单示例

    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();
        }
    });

希望这能让你清楚

答案 3 :(得分:0)

在此,结束日期将是用户选择的开始日期后的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();
        }
    };
}