Android:在DatePickerDialog中获取上一个日期

时间:2016-02-05 15:29:00

标签: android datepicker

我成功显示了DatePickerDialog,并使用datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());限制了上一个日期。在这种情况下,它不会激活之前的日期并限制前几个月,但用户仍然可以选择当前月份的上一个日期(了解查看图像和代码)

enter image description here

etDate.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            final Calendar mcurrentDate = Calendar.getInstance();
            int mYear = mcurrentDate.get(Calendar.YEAR);
            int mMonth = mcurrentDate.get(Calendar.MONTH);
            int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);


            DatePickerDialog datePickerDialog = new DatePickerDialog(EventMyEventEdit.this, new OnDateSetListener() {

                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear,
                        int dayOfMonth) {
                     mcurrentDate.set(Calendar.YEAR, year);
                     mcurrentDate.set(Calendar.MONTH, monthOfYear);
                     mcurrentDate.set(Calendar.DAY_OF_MONTH,dayOfMonth);
                     String myFormat = "dd/MM/yy";
                     SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

                     etDate.setText(sdf.format(mcurrentDate.getTime()));

                }
            }, mYear, mMonth, mDay);

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

            datePickerDialog.show();

        }
    });

编辑:我想在选择之前的日期而不是当前日期时限制用户

2 个答案:

答案 0 :(得分:1)

我有同样的问题,并通过更改DatePickerDialog的主题来解决它。我不知道为什么它取决于我的情况下的主题,但它对我有用

使用以下代码

 DatePickerDialog _date = new DatePickerDialog(this, AlertDialog.THEME_HOLO_LIGHT, datePickerListener, myear, mmonth,
                        mday) {
                    @Override
                    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                        view.setSpinnersShown(true);
                        Calendar c = Calendar.getInstance();
                        int myear = c.get(Calendar.YEAR);
                        int mmonth = c.get(Calendar.MONTH);
                        int mday = c.get(Calendar.DAY_OF_MONTH);

                        if (year < myear)
                            view.updateDate(myear, mmonth, mday);

                        if (monthOfYear < mmonth && year == myear)
                            view.updateDate(myear, mmonth, mday);

                        if (dayOfMonth < mday && year == myear && monthOfYear == mmonth)
                            view.updateDate(myear, mmonth, mday);

                    }
                };

答案 1 :(得分:1)

感谢Dhiraj。我通过将代码更改为Dhiraj建议来解决问题

<强>进口

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import android.support.v7.app.AppCompatActivity;
import android.widget.DatePicker;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;

<强>代码

etDate.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                final Calendar mcurrentDate = Calendar.getInstance();
                int mYear = mcurrentDate.get(Calendar.YEAR);
                int mMonth = mcurrentDate.get(Calendar.MONTH);
                int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);

                DatePickerDialog datePickerDialog = new DatePickerDialog(
                        EventMyEventEdit.this, AlertDialog.THEME_DEVICE_DEFAULT_DARK, new OnDateSetListener() {

                            @Override
                            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                                mcurrentDate.set(Calendar.YEAR, year);
                                mcurrentDate.set(Calendar.MONTH, monthOfYear);
                                mcurrentDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                                String myFormat = "dd/MM/yy";
                                SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

                                etDate.setText(sdf.format(mcurrentDate.getTime()));

                            }
                        }, mYear, mMonth, mDay);


                datePickerDialog.getDatePicker().setMinDate(new Date().getTime() - 10000);

                datePickerDialog.show();

            }
        });