简体   繁体   English

如何从当前月份和年份的日期选择器显示以限制显示上一年和月份的android?

[英]How to display from current month and year in date picker to restrict showing previous year and month android?

I am using custom date picker where i am using like this. 我在使用像这样的地方使用自定义日期选择器。

public class DatePickersDialog extends DatePickerDialog implements OnDateChangedListener {

    private DatePickerDialog mDatePicker;

     public DatePickersDialog(Context context, OnDateSetListener callBack,
            int year, int monthOfYear, int dayOfMonth) {
        super(context,callBack, year, monthOfYear, dayOfMonth);
       mDatePicker = new DatePickerDialog(context,callBack, year, monthOfYear, dayOfMonth);
       mDatePicker.getDatePicker().init(year, monthOfYear, dayOfMonth, this);
        updateTitle(year, monthOfYear);

    }
    public void onDateChanged(DatePicker view, int year,
        int month, int day) {
    updateTitle(year, month);
    }
    private void updateTitle(int year, int month) {
        Calendar mCalendar = Calendar.getInstance();
        mCalendar.set(Calendar.YEAR, year);
        mCalendar.set(Calendar.MONTH, month);
        mDatePicker.setTitle(getFormat().format(mCalendar.getTime()));

    }  
    public DatePickerDialog getPicker(){
        return this.mDatePicker;
    }

    public SimpleDateFormat getFormat(){
    return new SimpleDateFormat("MMM, yyyy");
    };   

   }
Utils.class 


public static DatePickerDialog customDatePickers(Activity activity, DatePickerDialog.OnDateSetListener datePickerListener) {

    Calendar c = Calendar.getInstance();
    int cardYear = c.get(Calendar.YEAR);
    int cardMonth = c.get(Calendar.MONTH);
    int cardDay = c.get(Calendar.DAY_OF_MONTH);
    DatePickersDialog datePickersDialog = new DatePickersDialog(activity,datePickerListener,cardYear,cardMonth,cardDay);
    Date minDate = new Date();
    c.set(cardYear, cardMonth, cardDay - 1, 0, 0);
    minDate.setTime(c.getTimeInMillis());
    DatePickerDialog datePickerDialog = datePickersDialog.getPicker();

    try {
        Field[] datePickerDialogFields = datePickerDialog.getClass().getDeclaredFields();
        for (Field datePickerDialogField : datePickerDialogFields) {
            if (datePickerDialogField.getName().equals("mDatePicker")) {
                datePickerDialogField.setAccessible(true);
                DatePicker datePicker = (DatePicker) datePickerDialogField.get(datePickerDialog);
                Field datePickerFields[] = datePickerDialogField.getType()
                        .getDeclaredFields();
                for (Field datePickerField : datePickerFields) {
                    if ("mDayPicker".equals(datePickerField.getName()) || "mDaySpinner".equals(datePickerField.getName())) {
                        datePickerField.setAccessible(true);
                        Object dayPicker = new Object();
                        dayPicker = datePickerField.get(datePicker);
                        ((View) dayPicker).setVisibility(View.GONE);
                    }
                }
            }
        }
        datePickerDialog.show();
    } catch (Exception ex) {
    }
    return datePickerDialog;
}

 scanExpireDate = (CheckoutEditText) findViewById(R.id.scan_expire_date);
scanExpireDate.setTextView();



public void onDatePickerClick(View view){
            Utils.customDatePicker(this, datePickerListener);
    }

But it displays like this below pic. 但它显示如下图所示。 showing previous year and month. 显示上一年和一个月。 How to restrict that? 如何限制呢? 在此处输入图片说明

How to solove this? 如何爱这个呢?

Thanks in advance 提前致谢

Set its bounds using: 使用以下方法设置边界:

setMinDate(long minDate)
setMaxDate(long maxDate)

Where the argument is the usual number of milliseconds since January 1, 1970 00:00:00 in the default time zone. 其中,自变量是默认时区中自1970年1月1日00:00:00以来的通常毫秒数。 You'll still have to calculate these values of course, but that should be trivial to do with the Calendar class: just take the current date and add or substract x years. 当然,您仍然必须计算这些值,但这对于Calendar类来说应该是微不足道的:只需获取当前日期并减去x年即可。

EDIT: You can get the underlying DatePicker from a DatePickerDialog (by simply calling getDatePicker()) or in your code I think it's where you call DatePicker datePicker and just add rows like: 编辑:您可以从DatePickerDialog(通过简单地调用getDatePicker())获取基础的DatePicker或在您的代码中,我认为这是您调用DatePicker datePicker并在其中添加以下行的地方:

datePicker.setMinDate(<long variable>);

I fixed this by using this in custom date picker. 我通过在自定义日期选择器中使用它来解决此问题。 Hope it might help for someone 希望对别人有帮助

DatePickerDialog datePickerDialog = datePickersDialog.getPicker(); DatePickerDialog datePickerDialog = datePickersDialog.getPicker(); datePickerDialog.getDatePicker().setMinDate(minDate.getTime()); 。datePickerDialog.getDatePicker()setMinDate(minDate.getTime());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM