我有两个日期选择器,它们进入两个单独的编辑文本框。一个是出发日期,另一个是返回日期。我需要一个验证理论,以确保返回日期必须在出发日期之后..
谢谢您的帮助
这是我的代码:
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 的按钮。如果返回日期大于出发日期,我希望使按钮不可点击。