2016-07-02 49 views
0

我有兩個編輯文本框一個用於檢入和另一個用於檢出日期。設置一個DatePickerDialog分鐘日高於其他DatePickerDialog

我已經採取了兩個DatePickerDialog,它會打開時,你點擊編輯文本框,這是行之有效的,但我需要它來設置,所以當選擇一個日期檢查編輯文本框然後檢查out datepicker應該從入住日期的第二天開始。

即您的退房日期不能在您入住日期之前。

my current code: 

private void setDateTimeField() { 
     checkInEdit.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
      @Override 
      public void onFocusChange(View v, boolean hasFocus) { 
       if (hasFocus){ 
        fromDatePickerDialog.show(); 

       } 
      } 
     }); 

     checkOutEdit.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
      @Override 
      public void onFocusChange(View v, boolean hasFocus) { 
       if (hasFocus) { 
        toDatePickerDialog.show(); 
       } 
      } 
     }); 

     Calendar newCalendar = Calendar.getInstance(); 
     fromDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { 

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

       newDate.add(Calendar.DATE, 1); 

       toDatePickerDialog.getDatePicker().setMinDate(newDate.get(Calendar.MILLISECOND)); 
      } 

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


     toDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { 

      public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 
       Calendar newDate = Calendar.getInstance(); 
       newDate.set(year, monthOfYear, dayOfMonth); 
       checkOutEdit.setText(dayOfMonth + "-" + monthOfYear + "-" + year); 
      } 

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


     fromDatePickerDialog.getDatePicker().setMinDate(newCalendar.get(Calendar.MILLISECOND)); 
     toDatePickerDialog.getDatePicker().setMinDate(newCalendar.get(Calendar.MILLISECOND)); 
    } 

我添加了這行

toDatePickerDialog.getDatePicker().setMinDate(newDate.get(Calendar.MILLISECOND)); 
fromDatePickerDialog onDateSet方法

,然而,這似乎並不堅持,即當我打開toDatePickerDialog它的日期仍設置爲今天。

回答

0

我沒有測試過,但嘗試下面的代碼,如果它的工作原理,

首先需要一個全局變量從日期壓延數據類型爲象下面這樣:

 Calendar fromDate; 

現在從日期選擇請從以下選定日期開始設置此日期

 // In your fromDatePickerDialog at onDateSet method 
    fromDate = newDate; 

現在改變你的重點列表的下面的代碼到時間編輯文本

 checkOutEdit.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      if (hasFocus) { 

       if(fromDate != null) 
         toDatePickerDialog.setMinDate(fromDate.getTime()); 

       toDatePickerDialog.show(); 
      } 
     } 
    });