android中的datepicker实现不允许用户选择过去的日期而不是今天

时间:2014-12-24 11:51:48

标签: android android-datepicker

发生了什么:: 我的日期选择器允许我输入日期

我想做什么:: 如何确保日期选择器显示(月,日,年)仅大于或等于现有

日期采摘实施:

//*******Date picker implementation******(Start)***//
    public void selectDate(View view) {
        DialogFragment fragment = new SelectDateFragment();
        fragment.show(getFragmentManager(), "DatePicker");
    }

    public void populateSetDate(int year, int month, int day) {
        txtDateId = (TextView) getActivity().findViewById(R.id.txtDateId);
        txtDateId.setText(month+"/"+day+"/"+year);
    }

    public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            final Calendar calendar = Calendar.getInstance();
            int yy = calendar.get(Calendar.YEAR);
            int mm = calendar.get(Calendar.MONTH);
            int dd = calendar.get(Calendar.DAY_OF_MONTH);
            return new DatePickerDialog(getActivity(), this, yy, mm, dd);
        }

        public void onDateSet(DatePicker view, int yy, int mm, int dd) {
            populateSetDate(yy, mm+1, dd);
        }
    }
    //*******Date picker implementation******(End)***//

3 个答案:

答案 0 :(得分:1)

将DatePicker对话框设置为当前日期,你可以这样

date.getDatePicker().setMaxDate(System.currentTimeMillis());

希望它有帮助...:)

答案 1 :(得分:0)

试试这个。希望它有所帮助!!!

Calendar calendar = Calendar.getInstance(); 
int currentYear = calendar.get(Calendar.YEAR); 
int currentMonth = calendar.get(Calendar.MONTH); 
int currentDay = calendar.get(Calendar.DAY_OF_MONTH);

DatePickerDialog datePickerDialog = new DatePickerDialog(this, listener, currentYear, currentMonth, currentDay);  
 DatePicker dp = datePickerDialog.getDatePicker();  
dp.setMinDate(calendar.getTimeInMillis()); 
return datePickerDialog;

答案 2 :(得分:0)

首先定义这些变量:

private int current_year, year;
private int current_month, month;
private int current_day, day;

然后,只需获取这样的当前日期,并在onCreate方法中分配值:

final Calendar c = Calendar.getInstance();
current_year = year = c.get(Calendar.YEAR);
current_month = month = c.get(Calendar.MONTH);
current_day = day = c.get(Calendar.DAY_OF_MONTH);

最后,只需用以下对话框替换对话框:     DatePickerDialog datePickerDialog = new DatePickerDialog(SignUpActivity.this,new DatePickerDialog.OnDateSetListener()             {

            @Override
            public void onDateSet(DatePicker view, int setYear, int setMonth, int setDay)
            {

                year = setYear;
                month = setMonth;
                day = setDay;
                btnDOB.setText(new StringBuilder().append(day).append("/").append(month + 1).append("/").append(year));

                etContactNumber.requestFocus();
            }

        }, year, month, day)
        {
            @Override
            public void onDateChanged(DatePicker view, int mYear, int mMonth, int mDay)
            {
                if (mYear > current_year)
                    view.updateDate(current_year, current_month, current_day);

                if (mMonth > current_month && mYear == current_year)
                    view.updateDate(current_year, current_month, current_day);

                if (mDay > current_day && mYear == current_year && mMonth == current_month)
                    view.updateDate(current_year, current_month, current_day);

            }
        };

        datePickerDialog.show();