Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在日期选择器中禁用上一个日期?_Android_Datepicker_Gregorian Calendar - Fatal编程技术网

Android 如何在日期选择器中禁用上一个日期?

Android 如何在日期选择器中禁用上一个日期?,android,datepicker,gregorian-calendar,Android,Datepicker,Gregorian Calendar,我使用的是公历,我需要在android中禁用前一个日期,直到当前日期。我已经检查了setMinDate方法,但它不起作用。。有人能帮我吗?这是我的密码 public void SelectDateTime() { final View dialogView = View.inflate(getApplicationContext(), R.layout.date_picker_activate, null); DateTimeDialog = new Alert

我使用的是公历,我需要在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());
使用此代码

    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");
此行设置了mindate-dialog.getDatePicker().setMinDate(c.getTimeInMillis())

并使用此设置最大日期

getDatePicker().setMaxDate(c.getTimeInMillis())


这将禁用previouis日期

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();
    }
使用此库

implementation 'com.github.florent37:singledateandtimepicker:1.2.1' 
并使用以下代码启动

 startSingleDateAndTimePickerDialog = new SingleDateAndTimePickerDialog.Builder(context);
            startSingleDateAndTimePickerDialog
                    .bottomSheet()
                    .curved()
                    .displayHours(true)
                    .displayMinutes(true)
                    .minDateRange(new Date(minDate))
                    .defaultDate(new Date(defaultDate))
                    .mustBeOnFuture()
.displayListener(new SingleDateAndTimePickerDialog.DisplayListener() {
                    @Override
                    public void onDisplayed(SingleDateAndTimePicker picker) {
                        //retrieve the SingleDateAndTimePicker
                    }
                })

                .title("Date & Time")
                .listener(new SingleDateAndTimePickerDialog.Listener() {
                    @Override
                    public void onDateSelected(Date date) {

                        Date currentDate = new Date(System.currentTimeMillis());



     String dateTime = getDate(date.getTime() / 1000);
textview.settext(dateTime);

                    }
                }).display();






 public static String getDate(long timeStamp) {
        Date time = new Date(timeStamp*1000);
        SimpleDateFormat df2 = new SimpleDateFormat("MMM dd,yyy h:mm a");
        return df2.format(time);
    }

您可以使用-,并设置最小和最大日期和时间,也可以移动代码并尝试此代码。兄弟,我需要时间选择器和这个日历。。你能帮我吗?但是这里的txt_日期应该是静态的,我不想把它设置为静态的。请给我一个解决方案,在这里我必须设置文本查看日期