禁用在DatePicker中滑动到上个月

时间:2016-09-20 11:51:56

标签: android datepicker android-datepicker

他们是否可以在过去一个月禁用Datepicker刷卡?

sample

我遇到了这个linkthis。但它似乎不会禁用刷卡/回到上个月。

原因:在当前/未来日期设置任务。

1 个答案:

答案 0 :(得分:1)

您可以创建自己的DatePickerDialog类。然后在调用datePicker时以毫秒电流传递必要的日期。 <{1}}方法会停用前一天,也会停用滑动。

DatePickerDialog类:

setMinDate()

活动内部:

public class myDatePickerDialogFragment extends DialogFragment {

    int mDay, mMonth, mYear;
    long current;

    // you also need to pass OnDateSetListener that is implemented in
    // activity which is calling this datePicker
    OnDateSetListener onSetDate;

    public myDatePickerDialogFragment(){
    }

    @Override
    public void setArguments(Bundle args) {
        super.setArguments(args);
        mDay = args.getInt("day");
        mMonth = args.getInt("month");
        mYear = args.getInt("year");
        current = args.getLong("minDate");
    }

    public void setOnDateSetListener(OnDateSetListener setDate){
        onSetDate = setDate;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), onSetDate, mYear, mMonth, mDay);

        // here you set minimum day. User cannot chose earlier dates.
        datePickerDialog.getDatePicker().setMinDate(current);

        return datePickerDialog;
    }
}

OnDateSetListener:

Bundle args = new Bundle();
args.putInt("day", mDay);
args.putInt("month", mMonth);
args.putInt("year", mYear);
args.putLong("minDate", yourCalendarObject.getTimeInMillis());

myDatePickerDialogFragment dialog = new myDatePickerDialogFragment();
dialog.setArguments(args);
dialog.setOnDateSetListener(datePickerListener);
dialog.show(getSupportFragmentManager(), "date");