2016-11-22 98 views
0

我正在創建一個應用程序,其中我顯示了日期選取器和時間選取器與驗證。我驗證了時間選取器,因爲我將當前時間與選定時間進行比較並設置消息「選擇正確時間」它選擇當前日期時正確工作,但當我選擇另一個日期時,它也顯示「選擇正確的時間」。我想以這種方式使它當我選擇另一個日期我應該允許我選擇任何時間。我知道我必須根據日期選擇進行設置,但我不知道如何設置。根據日期選擇驗證在選中日期

Calender c = Calendar.getInstance(); 
    year = c.get(Calendar.YEAR); 
    month = c.get(Calendar.MONTH); 
    day = c.get(Calendar.DAY_OF_MONTH); 
    hourofday = c.get(Calendar.HOUR_OF_DAY); 
    minuteofday = c.get(Calendar.MINUTE); 
    totalCurrenttime = hourofday + minuteofday; 



private void showDateDailog() { 

    final DatePickerDialog datePickerDialog = new DatePickerDialog(mContext, new DatePickerDialog.OnDateSetListener() { 

     @Override 
     public void onDateSet(DatePicker datePicker, int selectedYear, int selectedMonth, int selectedDate) { 

      year = selectedYear; 
      month = selectedMonth; 
      day = selectedDate; 


       ((TextView) findViewById(R.id.textViewTORStartDate)).setText(new StringBuilder().append(day).append("/") 
         .append(month + 1).append("/").append(year)) 
     } 
    }, year, month, day); 
    datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis()); 
    datePickerDialog.show(); 
} 

private void showTimeDailog() { 
    TimePickerDialog timePickerDialog = new TimePickerDialog(mContext, new TimePickerDialog.OnTimeSetListener() { 
     @Override 
     public void onTimeSet(TimePicker view, int selectedHour, int selectedMinute) { 

      hour = selectedHour; 
      minute = selectedMinute; 
      int selectedTime = hour + minute; 

       if (validTime(totalCurrenttime, selectedTime)) { 
        ((TextView) findViewById(R.id.textViewTORStartTime)).setText(selectedHour + ":" + selectedMinute); 
       } else { 
        Toast.makeText(getApplicationContext(), R.string.txt_select_correct_time, Toast.LENGTH_SHORT).show(); 
        ((TextView) findViewById(R.id.textViewTORStartTime)).setText(R.string.txt_Start_Time); 
       }     
     } 
    }, hour, minute, timeview); 
    timePickerDialog.show(); 
} 


public boolean validTime(long current, long selected) { 

    boolean isValid = false; 
    if (selected > current) { 
     isValid = true; 
    } 
    return isValid; 
} 

回答

0

在你validTime方法,你也必須經過它是由DatePicker控件設置日期,並將其與now比較。如果是未來日期,則返回true,而不進行比較時間。

例如

public boolean validTime(long current, long selected, long dateSet) { 

    if (dateSet > System.currentTimeMillis()) { 
     return true; 
    } else { 
     boolean isValid = false; 
     if (selected > current) { 
      isValid = true; 
     } 
     return isValid; 
    } 
} 

而在你onTimeSet方法:

public void onTimeSet(TimePicker view, int selectedHour, int selectedMinute) { 

    hour = selectedHour; 
    minute = selectedMinute; 
    int selectedTime = hour + minute; 

    Calendar cal = Calendar.getInstance(); 
    cal.set(year, month, day); 

    if (validTime(totalCurrenttime, selectedTime, cal.getTimeInMillis())) { 
     ((TextView) findViewById(R.id.textViewTORStartTime)).setText(selectedHour + ":" + selectedMinute); 
    } else { 
     Toast.makeText(getApplicationContext(), R.string.txt_select_correct_time, Toast.LENGTH_SHORT).show(); 
     ((TextView) findViewById(R.id.textViewTORStartTime)).setText(R.string.txt_Start_Time); 
    } 
} 
0

我同意@pleft,與您的驗證問題是由於你比較不採取日期考慮。編寫涉及的軟件時間是一件非常困難的事情,所以要利用庫來簡化這個過程。如果你使用java類,你可以用更簡單的方式得到你想要的結果。

從代碼看來,您似乎希望讓用戶選擇在現在爲之後的日期和時間。 Calendar類可以提供幫助。

// initializing the date to the current time 
private Calendar date = Calendar.getInstance(); 

// creating a date formatter to output the dateTime in the desired format 
private static final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); 

private void showDateDialog() { 
    Calendar c = GregorianCalendar.getInstance(); 
    final DatePickerDialog datePickerDialog = new DatePickerDialog(mContext, dateSetListener,c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE)); 
    datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis()); 
    datePickerDialog.show(); 
} 

// pulling out the listener for clarity 
private DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() { 
    @Override 
    public void onDateSet(DatePicker datePicker, int selectedYear, int selectedMonth, int selectedDate) { 
     date = new GregorianCalendar(selectedYear, selectedMonth, selectedDate); 
     String dateString = sdf.format(date.getTime()); 
     ((TextView) findViewById(R.id.textViewTORStartDate)).setText(dateString); 
    } 
}; 

private void showTimeDialog() { 
    Calendar c = Calendar.getInstance(); 
    TimePickerDialog timePickerDialog = new TimePickerDialog(mContext, timeSetListener, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), timeView); 
    timePickerDialog.show(); 
} 

// pulling out the listener for clarity 
private TimePickerDialog.OnTimeSetListener timeSetListener =new TimePickerDialog.OnTimeSetListener() { 

    @Override 
    public void onTimeSet(TimePicker view, int selectedHour, int selectedMinute) { 
     // creating a clone of the existing date 
     Calendar newCal = Calendar.class.cast(date.clone()); 
     // setting the hour & minute of the new calendar 
     newCal.set(Calendar.HOUR_OF_DAY, selectedHour); 
     newCal.set(Calendar.MINUTE, selectedMinute); 

     if (validTime(Calendar.getInstance(), newCal)) { 
      ((TextView) findViewById(R.id.textViewTORStartTime)).setText(selectedHour + ":" + selectedMinute); 
     } else { 
      Toast.makeText(getApplicationContext(), R.string.txt_select_correct_time, Toast.LENGTH_SHORT).show(); 
      ((TextView) findViewById(R.id.textViewTORStartTime)).setText(R.string.txt_Start_Time); 
     } 
    } 
}; 

public boolean validTime(Calendar current, Calendar selected) { 
    return 0 < selected.compareTo(current); 
} 

從這個主要的外賣是利用該java.util.Calendar類給你的權力的。我強烈建議看看Calendar docotutorials。另外,請記住,格式化自己的日期/時間是可以避免的。如果你堅持使用普通的java庫,SimpleDateFormat是你的朋友。 link

祝你好運!