在DatePickerDialog中设置最小日期

时间:2015-04-30 17:27:56

标签: android datepicker android-datepicker android-date

我想从明天开始设置DatePicker对话的最小日期。例如,如果今天是2015年4月30日,则应从2015年5月1日开始。

我的代码:

final Calendar myCalendar = Calendar.getInstance();

    final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            // TODO Auto-generated method stub
            myCalendar.set(Calendar.YEAR, year);
            myCalendar.set(Calendar.MONTH, monthOfYear);
            myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        }
    };

    startDate.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

        DatePickerDialog dialog = new DatePickerDialog(
                    SomeClassName.this, date, myCalendar
                            .get(Calendar.YEAR), myCalendar
                            .get(Calendar.MONTH), myCalendar
                            .get(Calendar.DAY_OF_MONTH));
        dialog.show();
        dialog.getDatePicker().setMinDate(
                        System.currentTimeMillis() - 10000);
    }});

我分别面临两个问题。

  1. 日期范围从当前日期开始,即2015年4月30日。

  2. 无论我通过滚动日期选择器选择什么日期,都只会在2015年4月30日设置。

  3. 注意:日期选择器对话框会弹出editText Box的触摸事件。

2 个答案:

答案 0 :(得分:1)

public class DatePickerFragment extends DialogFragment implements
            DatePickerDialog.OnDateSetListener {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the tommorrow date as the default date in the picker
            final Calendar c = Calendar.getInstance();
            c.add(Calendar.DAY_OF_MONTH, 1);
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);

            // Create a new instance of DatePickerDialog and return it
            DatePickerDialog dp = new DatePickerDialog(getActivity(), this, year, month, day);
            dp.getDatePicker().setMinDate(c.getTimeInMillis());
            return dp;
        }

        public void onDateSet(DatePicker view, int year, int month, int day) {
            // Do something with the date chosen by the user
        }
    }

显示日期选择器:

public void showDatePickerDialog() {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getSupportFragmentManager(), "datePicker");
}

有关详细信息,请访问:http://developer.android.com/guide/topics/ui/controls/pickers.html#DatePicker

答案 1 :(得分:0)

虽然上述建议是正确的,但我是以不同的方式做到的。从我这边发布解决方案。

final Calendar myCalendar = Calendar.getInstance();
myCalendar.set(Calendar.DAY_OF_MONTH+1);

final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

    @Override 
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        // TODO Auto-generated method stub 
        myCalendar.set(Calendar.YEAR, year);
        myCalendar.set(Calendar.MONTH, monthOfYear);
        myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        updateLabel();
    } 

}; 

startDate.setOnTouchListener(new OnTouchListener() { 
    @Override 
    public boolean onTouch(View v, MotionEvent event) {

    DatePickerDialog dialog = new DatePickerDialog(
                SomeClassName.this, date, myCalendar
                        .get(Calendar.YEAR), myCalendar
                        .get(Calendar.MONTH), myCalendar
                        .get(Calendar.DAY_OF_MONTH));
    dialog.show();
    dialog.getDatePicker().setMinDate(
                    myCalendar.getTimeInMillis());
}}); 


private void updateLabel()
{
   //Do some stuff
}