2015-08-08 92 views
0

我使用日曆EditText單擊以選擇用戶的出生日期並將其設置在EditText。它適用於所有設備。但只有在刷卡音速4.1.2(API 16),這表明年高達1980用戶不能選擇今年比1980年Android日曆選擇年

這下是我的代碼:

demo.setOnFocusChangeListener(new View.OnFocusChangeListener() { 

public void onFocusChange(View v, boolean hasFocus) { 
if (hasFocus) { 
DateDialog(); 
} 
} 
}); 



public void DateDialog() { 

     try { 
      DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() { 

       @Override 
       public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 

        demo.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year); 
        edt_contact.requestFocus(); 
        // checking for age should be 18 years and above 
        Calendar userAge = new GregorianCalendar(year, monthOfYear, dayOfMonth); 
        Calendar minAdultAge = new GregorianCalendar(); 
        minAdultAge.add(Calendar.YEAR, -18); 
        if (minAdultAge.before(userAge)) { 
         Toast.makeText(getApplicationContext(), "Age should be 18 and above", Toast.LENGTH_LONG).show(); 

         demo.setText(""); 
         // demo.requestFocus(); 
        } 

       } 
      }; 

      DatePickerDialog dpDialog = new DatePickerDialog(this, listener, year, month, day); 
      dpDialog.setCancelable(true); 
      dpDialog.show(); 

     } catch (Exception e) 

     { 

     e.printStackTrace(); 
     logsdata.logsdata("MainActivty", "DateDialog", e.getMessage().toString()); 
    } 

}//DateDialog 

回答

0

如果定位API> = 11,你可以嘗試設置最小日期,像這樣:

DatePickerDialog dpDialog = new DatePickerDialog(this, listener, year, month, day); 
dpDialog.setCancelable(true); 
dpDialog.show(); 
dpDialog.getDatePicker().setMinDate(0); //This will set the minimum date to 01/January/1970 
+0

是的,它的工作原理。但是我想分到1900年。因爲我想用出生日期。 –

+0

我沒有測試這個,但你可能可以將'minDate'設置爲'-2208988800',試試吧,讓我知道它是否有效:) –

+0

它顯示直到1970年只有不少於這個 –