簡體   English   中英

如何在Android DatePicker中禁用以前的日期?

[英]How to disable previous dates in Android DatePicker?

我想在DatePicker禁用過去的日期,只顯示當前日期和將來的日期...我正在使用下面的代碼,但這也會顯示過去的日期,請提出所需的更改。

date_btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
             Calendar calendar = Calendar.getInstance();
                mDay = calendar.get(Calendar.DAY_OF_MONTH);
                mMonth = calendar.get(Calendar.MONTH);
                mYear = calendar.get(Calendar.YEAR);

            OnDateSetListener callBack = new OnDateSetListener() {

                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear,
                        int dayOfMonth) {
                    Calendar calender_check = Calendar.getInstance(Locale.getDefault());
                    calender_check.set(Calendar.YEAR, year);
                    calender_check.set(Calendar.MONTH, monthOfYear);
                    calender_check.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                    SimpleDateFormat sdf2 = new SimpleDateFormat("MM/dd/yyyy");
                    Date date2 =calender_check.getTime();


                    updatedate(sdf2.format(date2),originalFormat.format(date2));
                    Log.d("Date >>>>", ""+date2);

                    Log.d("sel_date", sdf2.format(date2));

                    date_btn.setText(sdf2.format(date2));
                }
            };
            DatePickerDialog dialog = new DatePickerDialog(Class.this, callBack, mYear, mMonth, mDay);

            dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if(which == DialogInterface.BUTTON_NEGATIVE) {
                   // Log.i(TAG, "Cancel");
                    //isCancelled = true;
                    dialog.dismiss();}
                }
            });
            dialog.setCancelable(false);
            dialog.show();

        }
    });

您可能可以嘗試這樣的代碼:

DatePicker view; final long time = System.currentTimeMillis() - 1; //Set min time to now view.setMinDate(time);

在創建DatePickerDialog之前,請設置最小日期,然后顯示它。

DatePickerDialog datePickerDialog = new  DatePickerDialog(MainActiviy.this,date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),   myCalendar.get(Calendar.DAY_OF_MONTH));
SimpleDateFormat simpledateformate = new SimpleDateFormat("dd/MM/yyyy");
Date date = simpledateformate.parse("21/12/2012");
datePickerDialog.getDatePicker().setMinDate(d.getTime());
datePickerDialog.show();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM