禁用自定义android datepicker中的上一个日期

时间:2016-03-10 03:46:13

标签: android datepicker

我想在rey5137的datepicker对话框中禁用上一个日期,这是代码

@Override
    public void onClick(View v) {
        Dialog.Builder builder = null;

        builder = new DatePickerDialog.Builder(R.style.Material_App_Dialog_DatePicker){
            @Override
            public void onPositiveActionClicked(DialogFragment fragment) {
                DatePickerDialog dialog = (DatePickerDialog)fragment.getDialog();
                String date = dialog.getFormattedDate(SimpleDateFormat.getDateInstance());
                pilih_tgl.setText(date);
                super.onPositiveActionClicked(fragment);
            }

            @Override
            public void onNegativeActionClicked(DialogFragment fragment) {
                super.onNegativeActionClicked(fragment);
            }
        };

        builder.positiveAction("OK")
                .negativeAction("CANCEL");

        DialogFragment fragment = DialogFragment.newInstance(builder);

        fragment.show(getFragmentManager(), null);
    }

我应该将setMinDate放在我的代码中? 有什么帮助吗?

3 个答案:

答案 0 :(得分:2)

你可以做到

datePicker.setMinDate(System.currentTimeMillis() - 1000);

将今天的日期设为最低日期,所有过去日期均已停用。

datePickerDatePicker的对象,如果您使用DatePickerDialog的对象,则可以执行

datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);

答案 1 :(得分:1)

您需要将dateRange添加到构建器。您可以根据需要设置maxDate。

Dialog.Builder builder = null;
                Calendar c = Calendar.getInstance();
              int year =  c.get(Calendar.YEAR)+200;
                c.set(year,c.get(Calendar.MONTH),c.get(Calendar.DATE));
                builder = new DatePickerDialog.Builder(R.style.Material_App_Dialog_DatePicker) {
                    @Override
                    public void onPositiveActionClicked(DialogFragment fragment) {
                        DatePickerDialog dialog = (DatePickerDialog) fragment.getDialog();
                        String date = dialog.getFormattedDate(SimpleDateFormat.getDateInstance());
                        //pilih_tgl.setText(date);
                        super.onPositiveActionClicked(fragment);
                    }

                    @Override
                    public void onNegativeActionClicked(DialogFragment fragment) {
                        super.onNegativeActionClicked(fragment);
                    }
                }.dateRange(System.currentTimeMillis() - 1000, c.getTimeInMillis());
                builder.positiveAction("OK")
                        .negativeAction("CANCEL");
                DialogFragment fragment = DialogFragment.newInstance(builder);

                fragment.show(getFragmentManager(), null);
            }
        });

答案 2 :(得分:0)

您可以按以下方式更改代码:

@Override
public void onClick(View v) {
    Dialog.Builder builder = null;

    builder = new DatePickerDialog.Builder(R.style.Material_App_Dialog_DatePicker){
        @Override
        public void onPositiveActionClicked(DialogFragment fragment) {
            DatePickerDialog dialog = (DatePickerDialog)fragment.getDialog();
            dialog = dialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
            String date = dialog.getFormattedDate(SimpleDateFormat.getDateInstance());
            pilih_tgl.setText(date);
            super.onPositiveActionClicked(fragment);
        }

        @Override
        public void onNegativeActionClicked(DialogFragment fragment) {
            super.onNegativeActionClicked(fragment);
        }
    };

    builder.positiveAction("OK")
            .negativeAction("CANCEL");

    DialogFragment fragment = DialogFragment.newInstance(builder);

    fragment.show(getFragmentManager(), null);
}