如何在日期选择器中禁用预设日期?

时间:2018-06-08 11:31:49

标签: android datepicker gregorian-calendar

我正在使用格里高利历,我需要在android中禁用之前的日期直到当前日期。我已经检查了setMinDate方法,但它无法工作..任何人都可以帮助我吗?这是我的代码..

 public void SelectDateTime() {
        final View dialogView = View.inflate(getApplicationContext(), R.layout.date_picker_activate, null);
        DateTimeDialog = new AlertDialog.Builder(getApplicationContext()).create();
        dialogView.findViewById(R.id.cancelBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DateTimeDialog.dismiss();
            }
        });
        dialogView.findViewById(R.id.date_time_set).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.datePicker1);
                TimePicker timePicker = (TimePicker) dialogView.findViewById(R.id.timePicker);
                Calendar calendar = new GregorianCalendar(datePicker.getYear(),
                        datePicker.getMonth(),
                        datePicker.getDayOfMonth(),
                        timePicker.getCurrentHour(),
                        timePicker.getCurrentMinute());

                datePicker.getMinDate();

                final long today1 = System.currentTimeMillis() - 1000;

                calendar.after(System.currentTimeMillis());

               // datePicker.setMinDate(calendar.getTimeInMillis());

3 个答案:

答案 0 :(得分:0)

使用此代码

    public static class DatePickerFragment extends DialogFragment
        implements DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        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);
        DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
        // dialog.getDatePicker().setMaxDate(c.getTimeInMillis());
        dialog.getDatePicker().setMinDate(c.getTimeInMillis());
        return dialog;
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        txt_date.setText(String.format(Locale.ENGLISH, "%02d/%02d/%d", day, month + 1, year));
    }
}

在侧面按钮点击中调用此方法,如

DialogFragment newFragment = new DatePickerFragment();
            newFragment.show(getActivity().getSupportFragmentManager(), "datePicker");
  

此行设置最小日期 - dialog.getDatePicker()。setMinDate(c.getTimeInMillis());

并使用此设置最大日期

  

dialog.getDatePicker()setMaxDate(c.getTimeInMillis());

答案 1 :(得分:0)

这将禁用上一个日期

public void showDateToSelect() {
        // Get Current Date
        final Calendar c = Calendar.getInstance();
        int mYear = c.get(Calendar.YEAR);
        int mMonth = c.get(Calendar.MONTH);
        int mDay = c.get(Calendar.DAY_OF_MONTH);
        /*DatePicker dialog to select date*/
        DatePickerDialog datePickerDialog = new DatePickerDialog(mContext,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        textView.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
                    }
                }, mYear, mMonth, mDay);
        /*To show only dates from today to future*/
        datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
        datePickerDialog.show();
    }

答案 2 :(得分:0)

使用此库

SETLOCAL ENABLEDELAYEDEXPANSION
for /D %%a in (C:\BACKUP\*) do (
    SET var="%%a"
    echo The ServerName is :: !var! >>1.txt & echo.>>1.txt  
    for /R /D %%b in ("%%a\*") do (
        SET var1="%%b"
        SET var2="%%~nb"        
        echo the subfolder of  !var! is:: '' !var2!>>1.txt & echo.>>1.txt           
        for /f %%x in ('DIR /B /A-D /OD /T:C "%%b\*.bak"') do (set 
            bakFile="%%x"
        )
        echo The Latest Bak File is :: !bakFile!  >> 1.txt & echo.>>1.txt
    )
)

并使用以下代码启动

implementation 'com.github.florent37:singledateandtimepicker:1.2.1'