我有两个日期选择器,分为两个单独的编辑文本框。一个是离开日期,另一个是返回日期。我需要一个验证理论,确保返回日期必须在出发日期之后。
感谢您的帮助
这是我的代码:
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的按钮。如果返回日期大于离开日期,我将希望使按钮不可点击。
答案 0 :(得分:1)
我已经使用了这个可以用作属性的类:
[AttributeUsage(AttributeTargets.Property)]
public class DateGreaterThanAttribute : ValidationAttribute
{
public DateGreaterThanAttribute(string dateToCompareToFieldName)
{
DateToCompareToFieldName = dateToCompareToFieldName;
}
private string DateToCompareToFieldName { get; set; }
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
DateTime earlierDate = (DateTime)validationContext.ObjectType.GetProperty(DateToCompareToFieldName).GetValue(validationContext.ObjectInstance, null);
if ((DateTime)value != null)
{
DateTime? laterDate = (DateTime)value;
if (earlierDate <= laterDate)
{
return ValidationResult.Success;
}
else
{
return new ValidationResult("End date must be later than start date!");
}
}
else {return new ValidationResult("End date is expected.");}
}
}
然后,您可以将其用作日期字段的注释。
答案 1 :(得分:0)
这里有一个问题的答案: Date Comparison using Java
但如果日期未通过验证,您也可以将按钮设置为不启用。
ImageButton button = (ImageButton)findViewById(R.id.imageButton1);
if(!passesValidation) {
button.setEnabled(false);
}
希望这有帮助吗?
答案 2 :(得分:0)
请在下面找到java代码。我希望它会对你有所帮助。
public class ValidateDate{
private Pattern pattern;
private Matcher matcher;
private static final String PATTERN_OF_DATE =
"(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)";
public ValidateDate(){
pattern = Pattern.compile(PATTERN_OF_DATE);
}
public boolean DateValidate(final String date){
matcher = pattern.matcher(date);
if(matcher.matches()){
matcher.reset();
if(matcher.find()){
String dayIs = matcher.group(1);
String monthIs = matcher.group(2);
int year = Integer.parseInt(matcher.group(3));
if (dayIs.equals("31") &&
(monthIs.equals("4") || monthIs .equals("6") || monthIs.equals("9") ||
monthIs.equals("11") || monthIs.equals("04") || monthIs .equals("06") ||
monthIs.equals("09"))) {
return false; // only 1,3,5,7,8,10,12 has 31 days
} else if (monthIs.equals("2") || monthIs.equals("02")) {
//leap year
if(year % 4==0){
if(dayIs.equals("30") || dayIs.equals("31")){
return false;
}else{
return true;
}
}else{
if(dayIs.equals("29")||dayIs.equals("30")||dayIs.equals("31")){
return false;
}else{
return true;
}
}
}else{
return true;
}
}else{
return false;
}
}else{
return false;
}
}
}