2017-08-16 103 views
0

請幫幫我。當我的DatePickerDialog顯示年份設置爲'1900'時。我只知道如何限制過去的日期,但我不知道如何限制過去的幾年。 我想要發生的是如何在Android中設置DatePickerDialog中的當前年份?

ex。用戶想要輸入檢查/檢查日期,我只會允許過去的日期到當年。

public void ChequeDate(View view) { 
    DatePickerDialog dpd = new DatePickerDialog(PaymentHeader.this, 
      new DatePickerDialog.OnDateSetListener() { 
       @Override 
       public void onDateSet(DatePicker view, int year, 
             int monthOfYear, int dayOfMonth) { 
        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); 

        if (monthOfYear < 9) { 
         if (dayOfMonth < 10) { 
          ChequeDateET.setText(year + "-0" 
            + (monthOfYear + 1) + "-0" + dayOfMonth); 
         } else { 
          ChequeDateET.setText(year + "-0" 
            + (monthOfYear + 1) + "-" + dayOfMonth); 
         } 
        } else { 
         if (dayOfMonth < 10) { 
          ChequeDateET.setText(year + "-" 
            + (monthOfYear + 1) + "-0" + dayOfMonth); 
         } else { 
          ChequeDateET.setText(year + "-" 
            + (monthOfYear + 1) + "-" + dayOfMonth); 
         } 
        } 
       } 
      }, mYear, mMonth, mDay); 
    //dpd.getDatePicker().setMinDate(System.currentTimeMillis() - 1000); 
    dpd.show(); 
} 

如果您需要說明,請詢問。謝謝您的幫助。

回答

0

您必須獲得current calendar instance,並且由此您必須獲取year, month and date對象作爲DatePickerDialog方法中的參數傳遞並啓用setMinDate方法。 現在檢查下面的代碼

public void ChequeDate(View view) { 
    Calendar calendar = Calendar.getInstance(); 
    int date = calendar.get(Calendar.DATE); 
    int month = calendar.get(Calendar.MONTH); 
    int year = calendar.get(Calendar.YEAR); 
    DatePickerDialog dpd = new DatePickerDialog(PaymentHeader.this, 
      new DatePickerDialog.OnDateSetListener() { 
       @Override 
       public void onDateSet(DatePicker view, int year, 
             int monthOfYear, int dayOfMonth) { 
        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); 

        if (monthOfYear < 9) { 
         if (dayOfMonth < 10) { 
          ChequeDateET.setText(year + "-0" 
            + (monthOfYear + 1) + "-0" + dayOfMonth); 
         } else { 
          ChequeDateET.setText(year + "-0" 
            + (monthOfYear + 1) + "-" + dayOfMonth); 
         } 
        } else { 
         if (dayOfMonth < 10) { 
          ChequeDateET.setText(year + "-" 
            + (monthOfYear + 1) + "-0" + dayOfMonth); 
         } else { 
          ChequeDateET.setText(year + "-" 
            + (monthOfYear + 1) + "-" + dayOfMonth); 
         } 
        } 
       } 
      }, year, month, date); 
    dpd.getDatePicker().setMinDate(System.currentTimeMillis() - 1000); 
    dpd.show(); 
} 
相關問題