getDatePicker()。DatePickerDialog的setMinDate不起作用

时间:2016-01-29 14:42:38

标签: android android-calendar android-datepicker

我希望DatePickerDialog不显示当前日期之前的日期。我正在使用setMinDate(long l)方法。但它不起作用。

我使用内部类设置minDate:

public static class DatePickerFragment extends DialogFragment
        implements DatePickerDialog.OnDateSetListener {
    DatePickerDialog datePickerDialog;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        String s = month + day + year+"";
        datePickerDialog = new DatePickerDialog(getActivity(),this,year,month,day);
        datePickerDialog.getDatePicker().setMinDate(Long.parseLong(s));

        // Create a new instance of DatePickerDialog and return it
            return  datePickerDialog;
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        SelectedDateView.setText(day+ (month + 1) + "-"  + "-" + year);

    }
}

这是显示它的方法:

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

3 个答案:

答案 0 :(得分:1)

setMinDate()需要unix时间(自1970年1月1日00:00:00以来的毫秒数)。尝试这样的事情:

com.oldcurmudgeon.test.Test$Simple@15db9742

希望这有帮助。

答案 1 :(得分:0)

试试这个:

long time= System.currentTimeMillis();
datePickerDialog.getDatePicker().setMinDate(time);

答案 2 :(得分:0)

对于23岁以上的sdks,您必须使用" System.currentTimeMillis()"而不是" cal.getTimeInMillis()"。

sdk < 23: datePickerStart.getDatePicker().setMinDate(System.currentTimeMillis())
sdk > 23: datePickerStart.getDatePicker().setMinDate(cal.getTimeInMillis())