我有兩個日期選擇器進入兩個單獨的編輯文本框。一個是出發日期,另一個是回程日期。我需要一個驗證的理論,將確保返回日期必須在出發日期後..需要驗證退貨日期是否大於出發日期?
感謝您的幫助
這裏是我的代碼:
DepartDate = (TextView) findViewById(R.id.editText1);
ReturnDate = (TextView) findViewById(R.id.editText2);
public void selectDate(View view) { // Function used to set the date
switch(view.getId()) {
case R.id.imageButton1: // Using the first image button to call the first date picker.
DialogFragment newFragment1 = new SelectDateFragment(0); // Giving an index to the date picker so it won't overwrite the textfields
newFragment1.show(getSupportFragmentManager(), "DatePicker");
break;
case R.id.imageButton2: // Using the first image button to call the first date picker.
DialogFragment newFragment2 = new SelectDateFragment(1); // Giving an index to the date picker so it won't overwrite the textfields
newFragment2.show(getSupportFragmentManager(), "DatePicker");
break;
}
}
public void populateSetDate(int year, int month, int day) { // Setting the format of the date and setting where the selected date will be entered to
DepartDate = (TextView) findViewById(R.id.editText1);
DepartDate.setText(month + "/" + day + "/" + year); //Setting the format in which the date will be shown in the textview
}
public void populateSetDate1(int year1, int month1, int day1) {
ReturnDate = (TextView) findViewById(R.id.editText2);
ReturnDate.setText(month1 + "/" + day1 + "/" + year1);
}
public class SelectDateFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
int type;
public SelectDateFragment(int type) {
this.type = type;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, yy, mm, dd);
}
public void onDateSet(DatePicker view, int yy, int mm, int dd) {
if(type == 0) { //If the first date picker was clicked then call the following function
populateSetDate(yy, mm + 1, dd);
} else if(type == 1) { //If the second date picker was clicked then call the following function
populateSetDate1(yy, mm + 1, dd);
}
}
我也有一個按鈕與onClickListener。如果返回日期大於出發日期,我會希望使按鈕不可點擊。
你可以嘗試這樣的事情,正如我已經張貼在thsi問題http://stackoverflow.com/questions/15038015/how-to-ignore-time-while-checking-date-using-beforedate- d – Pragnani 2013-04-30 17:44:34