-1

在我的應用程序中,我有一個出生日期字段,我需要驗證...驗證條件是用戶不應選擇當前日期,如果選擇它顯示錯誤消息... 日期正在從DatePickerDialog以這種方式選擇:在Android中驗證出生日期字段

private void updateDisplay() { 
    tv_dob.setText(new StringBuilder() 
    // Month is 0 based so add 1 
      .append(mMonth + 1).append("-").append(mDay).append("-") 

      .append(mYear - 1).append(" ")); 
} 

private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() { 

    public void onDateSet(DatePicker view, int year, int monthOfYear, 
      int dayOfMonth) { 
     mYear = year; 
     mMonth = monthOfYear; 
     mDay = dayOfMonth; 
     updateDisplay(); 
    } 
}; 


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

    // set current date into textview 
    tv_applndate.setText(new StringBuilder() 
      // Month is 0 based, just add 1 
      .append(month + 1).append("-").append(day).append("-") 
      .append(year)); 

如何驗證這個日期欄?

+1

你試過了嗎?添加一些邏輯來檢查,例如'date NetStarter

+0

您也可以使用'setMaxDate(long maxDate)'(API等級11) –

+0

在變量中存儲今天的日期,即日期,月份和年份,並與您獲得的變量輸入用戶之後的日期對話框選擇器。 –

回答

0

你可以試試這個

et_dob.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      showDialog(DATE_DIALOG_ID); 
     } 

    }); 
    final Calendar calendar = Calendar.getInstance(); 
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 
    if (et_dob.getText().toString() != null) { 
     try { 
      calendar.setTime(df.parse(et_dob.getText().toString())); 
     } catch (java.text.ParseException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
     mYear = calendar.get(Calendar.YEAR); 
     mMonth = calendar.get(Calendar.MONTH); 
     mDay = calendar.get(Calendar.DAY_OF_MONTH); 
     SimpleDateFormat month_date = new SimpleDateFormat("MMM"); 
     month = month_date.format(calendar.getTime()); 
    } else { 
     mYear = calendar.get(Calendar.YEAR); 
     mMonth = calendar.get(Calendar.MONTH); 
     mDay = calendar.get(Calendar.DAY_OF_MONTH); 
     SimpleDateFormat month_date = new SimpleDateFormat("MMM"); 
     month = month_date.format(calendar.getTime()); 
    } 

    if (cal_currentTime.compareTo(calendar) > 0) 
     updateDisplay(); 
} 

private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() { 

    public void onDateSet(DatePicker view, int year, int monthOfYear, 
      int dayOfMonth) { 
     mYear = year; 
     mMonth = monthOfYear; 
     mDay = dayOfMonth; 

     String dateSetter = (new StringBuilder().append(mYear).append("-") 
       .append(mMonth + 1).append("-").append(mDay).append("")) 
       .toString(); 
     final Calendar cal = Calendar.getInstance(); 
     DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 
     if (dateSetter != null) { 
      try { 
       cal.setTime(df.parse(dateSetter)); 
      } catch (java.text.ParseException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
      SimpleDateFormat month_date = new SimpleDateFormat("MMM"); 
      month = month_date.format(cal.getTime()); 
     } 

     if (cal_currentTime.compareTo(cal) > 0) 
      updateDisplay(); 
     else 
      Toast.makeText(context, "Choose Proper date format", 
        Toast.LENGTH_SHORT).show(); 
    } 
}; 

private void updateDisplay() { 
    dateOfBirth = (new StringBuilder() 
      // Month is 0 based so add 1 
      .append(mYear).append("-").append(mMonth + 1).append("-") 
      .append(mDay).append("")).toString(); 
    et_dob.setText(new StringBuilder() 
      // Month is 0 based so add 1 
      .append(mDay).append("-").append(month).append("-") 
      .append(mYear)); 
} 
0

使用此驗證在ondateset compareTo方法返回1,如果大於-1若跌破功能小

Calendar cal = Calendar.getInstance(); 

     cal.set(year, month, day); 
     Date current = cal.getTime(); 

     int diff1 =new Date().compareTo(current); 

     if(diff1>0){ 
      Toast.makeText(ctx, "Please select a valid date.", Toast.LENGTH_LONG).show(); 
      return; 
     } 
1

粘貼和替換您的對話ID,按鈕的ID,並編輯文本ID最簡單的方式出生日期選擇器的日期。你可以使用正常的日期選擇器,如果你想刪除vaidation的未來日期它將作爲一個正常的日期選擇器

private void setDateTimeField() { 

    et_dob.setOnClickListener(this); 
    dateFormatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US); //dd/MM/yyyy 

    Calendar newCalendar = Calendar.getInstance(); 
    dobDatePickerDialog = new DatePickerDialog(getActivity(), new OnDateSetListener() { 

     public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 
      Calendar newDate = Calendar.getInstance(); 
      newDate.set(year, monthOfYear, dayOfMonth); 

      Date current = newDate.getTime(); 
      int diff1 =new Date().compareTo(current); 

       if(diff1<0){ 
        Toast.makeText(getActivity(), "Please select a valid date", Toast.LENGTH_LONG).show(); 
        return; 
       } 
       else{ 
        et_dob.setText(dateFormatter.format(newDate.getTime())); 
       } 

     } 

    },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH)); 


}