2013-04-05 13 views
5

Mam aplikacja, w której użytkownik ma do wyboru dwa terminy namely- od daty i do tej pory .Both pól nie powinna przyjąć dowolną datę mniej niż bieżąca data . Przejrzałem kilka postów na tej stronie i osiągnąłem tę funkcjonalność za pomocą pojedynczego okna dialogowego datepicker i pojedynczego ondatesetListener.Walidacja „z datą” i „na bieżąco” w android

moim problemem jest to, że nie jestem w stanie potwierdzić, że stan aktualne powinna być zawsze większa lub równa od daty .please pomóc me.I jestem w stanie uchwycić od daty jako aktywny data jest overide przez aktywny daty zakończenia Dzięki w advance.Following kod:

public class Leave_form extends Activity 
{  
    private static EditText tvDislpayResult; 
    private Button startPickDate; 
    private Button endPickDate; 
    private Calendar startDate; 
    private Calendar endDate; 
    //private EditText startDateDisplay; 
    //private EditText endDateDisplay; 
    static final int DATE_DIALOG_ID = 0; 
    private TextView startDateDisplay; 
    private TextView endDateDisplay; 
    private TextView activeDateDisplay; 
    private Calendar activeDate; 
    private Calendar currentDate; 
    private Calendar fromDate; 

    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_leave_form); 

     /* capture our View elements for the start date function */ 
     startDateDisplay = (EditText) findViewById(R.id.e_from_date); 
     startPickDate = (Button) findViewById(R.id.Set_date1); 

     /*get tday date */ 

     currentDate=Calendar.getInstance(); 
     Log.d("currentDate ",""+(currentDate.get(Calendar.DAY_OF_MONTH))+(currentDate.get(Calendar.MONTH)+1)+(currentDate.get(Calendar.YEAR))); 

     /* get the current date */ 
     startDate = Calendar.getInstance(); 
     Log.d("startdate ",""+(startDate.get(Calendar.DAY_OF_MONTH))+(startDate.get(Calendar.MONTH)+1)+(startDate.get(Calendar.YEAR))); 

     /* add a click listener to the button */ 
     startPickDate.setOnClickListener(new View.OnClickListener() 
     { 
      public void onClick(View v) 
      { 
       showDateDialog(startDateDisplay, startDate); 
       Log.d("startDate-on click ",""+(startDate.get(Calendar.DAY_OF_MONTH))+(startDate.get(Calendar.MONTH)+1)+(startDate.get(Calendar.YEAR))); 

      } 
     }); 

     /* capture our View elements for the end date function */ 
     endDateDisplay = (EditText) findViewById(R.id.e_to_date); 
     endPickDate = (Button) findViewById(R.id.Set_date2); 

     /* get the current date */ 
     endDate = Calendar.getInstance(); 
     Log.d("endDate ",""+(endDate.get(Calendar.DAY_OF_MONTH))+(endDate.get(Calendar.MONTH)+1)+(endDate.get(Calendar.YEAR))); 

     /* add a click listener to the button */ 
     endPickDate.setOnClickListener(new View.OnClickListener() 
     { 
      public void onClick(View v) 
      { 
       showDateDialog(endDateDisplay, endDate); 
       Log.d("endDate -on click",""+(endDate.get(Calendar.DAY_OF_MONTH))+(endDate.get(Calendar.MONTH)+1)+(endDate.get(Calendar.YEAR))); 
      } 
     }); 

     /* display the current date (this method is below) */ 
     // updateDisplay(startDateDisplay, startDate); 
     // updateDisplay(endDateDisplay, endDate); 
    } 

    private void updateDisplay(TextView dateDisplay, Calendar date) 
    { 
     dateDisplay.setText(
       new StringBuilder() 
        // Month is 0 based so add 1 
        .append(date.get(Calendar.DAY_OF_MONTH)).append("-") 
        .append(date.get(Calendar.MONTH)+1).append("-") 
        .append(date.get(Calendar.YEAR)).append(" ")); 

     Log.d("msg","date:"+(date.get(Calendar.DAY_OF_MONTH))+(date.get(Calendar.MONTH)+1)+(date.get(Calendar.YEAR))); 

     fromDate=date; 
    } 

    public void showDateDialog(TextView dateDisplay, Calendar date) 
    { 
     Log.d("SDD", dateDisplay.getText().toString()); 
     Log.d("startdate ",""+(currentDate.get(Calendar.DAY_OF_MONTH))+(currentDate.get(Calendar.MONTH)+1)+(currentDate.get(Calendar.YEAR))); 
     activeDateDisplay = dateDisplay; 
     activeDate = date; 
     showDialog(DATE_DIALOG_ID); 
    } 

    private OnDateSetListener dateSetListener = new OnDateSetListener() 
    { 
     @SuppressWarnings("deprecation") 
     @Override 
     public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 
      activeDate.set(Calendar.YEAR, year); 
      activeDate.set(Calendar.MONTH, monthOfYear); 
      activeDate.set(Calendar.DAY_OF_MONTH, dayOfMonth); 
      Log.d("activeDate-on dateset ",""+(activeDate.get(Calendar.DAY_OF_MONTH))+(activeDate.get(Calendar.MONTH)+1)+(activeDate.get(Calendar.YEAR))); 

      if(currentDate.after(activeDate)|(currentDate.equals(activeDate))) 
      { 
       Toast toast=Toast.makeText(Leave_form.this, "Please select a valid date", Toast.LENGTH_LONG); 
       toast.setGravity(Gravity.CENTER, 0, 0); 
       toast.show(); 


      } 
      else 
      { 
       if((currentDate.after(activeDate)|(currentDate.equals(activeDate)))&&(startDateDisplay.getText().toString()!=null)) 
       { 
        Log.d("startDate-on click ",""+(startDate.get(Calendar.DAY_OF_MONTH))+(startDate.get(Calendar.MONTH)+1)+(startDate.get(Calendar.YEAR))); 
        if(startDate.before(activeDate)) 
        { 
         updateDisplay(activeDateDisplay, activeDate); 
        } 

        else 
        { 
         Toast.makeText(getApplicationContext(), "enter valid date", Toast.LENGTH_SHORT).show(); 
         /*AlertDialog alertDialog=new AlertDialog.Builder(Leave_form.this).create(); 

         alertDialog.setMessage("Choose a valid date"); 

         alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 

          @Override 
          public void onClick(DialogInterface dialog, int which) { 
           // TODO Auto-generated method stub 

          } 
         }); 
         alertDialog.show();*/ 
        } 
       } 
       else 
       { 
        updateDisplay(activeDateDisplay, activeDate); 
       } 

      } 

      unregisterDateDisplay(); 
     } 
    }; 

    private void unregisterDateDisplay() 
    { 
     Log.d("startdate ",""+(currentDate.get(Calendar.DAY_OF_MONTH))+(currentDate.get(Calendar.MONTH)+1)+(currentDate.get(Calendar.YEAR))); 
     Log.d("from date:","-"+activeDate.DAY_OF_MONTH+activeDate.MONTH+activeDate.YEAR); 
     activeDateDisplay = null; 
     activeDate = null; 
    } 

    @Override 
    protected Dialog onCreateDialog(int id) { 
     switch (id) { 

      case DATE_DIALOG_ID: 
       Log.d("fromdate","msg"); 
       Log.d("id",""+DATE_DIALOG_ID); 
       return new DatePickerDialog(this, dateSetListener, activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH)); 
     } 
     return null; 
    } 

    @Override 
    protected void onPrepareDialog(int id, Dialog dialog) { 
     super.onPrepareDialog(id, dialog); 
     switch (id) { 
      case DATE_DIALOG_ID: 
       Log.d("id",""+DATE_DIALOG_ID); 
       ((DatePickerDialog) dialog).updateDate(activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH)); 
       break; 
     } 
    } 

    public void leave(View view) 
    { 
     Intent intent1=new Intent(this,Rough.class); 
     setContentView(R.layout.activity_rough); 
     startActivity(intent1); 
    } 
    public void logout(View view) 
    { 
     Intent intent2=new Intent (this,MainActivity.class); 
     setContentView(R.layout.activity_main); 
     startActivity(intent2); 
    } 
} 
+0

Zastosowanie SimpleDateFormat curFormater = nowy SimpleDateFormat ("rrrr MM-dd'T'GG: min: SS"); Data d1, d2; d1 = curFormater.parse (dbDateMod); d2 = curFormater.parse (wsDbDateMod); \t \t \t \t \t \t if (d1.before (D2)) \t \t \t \t \t \t \t \t \t { \t \t \t \t \t \t \t \t \t} – Amit

+0

chck moje rozwiązanie – Nezam

+0

dzięki @Amit ..ale wat to jest "dbDateMod"? – user2179989

Odpowiedz

2

lepiej zmienić te kody:

if((currentDate.after(activeDate)|(currentDate.equals(activeDate)))&&(startDateDisplay.getText().toString()!=null)) 

do wielokrotności jeśli bloków jak:

if((currentDate.after(activeDate)||(currentDate.equals(activeDate)) 
    if(!startDateDisplay.getText().toString().equals(null)) 

Uwaga uważnie różnic!

Zobacz również w tym answer here

+0

Dzięki @ Nezam..it poprawia mój kod. – user2179989

2

rozwiązany ..

Dzięki @Amit za problem jest rozwiązany answer.My i działa jak charm.I zmieniły się tylko kod w OnDateSetListener segment.Following to kod:

public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) 
    { 


     activeDate.set(Calendar.YEAR, year); 
     activeDate.set(Calendar.MONTH, monthOfYear); 
     activeDate.set(Calendar.DAY_OF_MONTH, dayOfMonth); 



     Date fromDate,toDate; 
     try 
      { 


       //checks if the date chosen by the user is greater than the current system date.Doesnt allow to choose past date 
       if(currentDate.before(activeDate)||currentDate.equals(activeDate)) 
       { 

        //checks if the from date edit text is empty or not 
        if(startDateDisplay.getText().toString().isEmpty()) 
        { 

         updateDisplay(activeDateDisplay, activeDate); 

        } 
        else 
        { 


         //converts the date in the from date edittextbox and active date to dd-mm-yyyy format 
         SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-yyyy"); 

         fromDate=sdf.parse(startDateDisplay.getText().toString()); 
         Log.d("fromdate",""+fromDate); 

         String temp=(activeDate.get(Calendar.DAY_OF_MONTH))+"-"+(activeDate.get(Calendar.MONTH)+1)+"-"+(activeDate.get(Calendar.YEAR)); 
         toDate=sdf.parse(temp); 
         Log.d("todate",""+toDate); 


         //checks if todate is greater or equal to from date 
         if ((toDate.after(fromDate))||(toDate.equals(fromDate))) 
         { 
          updateDisplay(activeDateDisplay, activeDate); 

         } 
         else 
         { 
          Toast toast=new Toast(getBaseContext()); 
          toast=Toast.makeText(Leave_form.this, "To date should be greater than From date", Toast.LENGTH_SHORT); 
          toast.setGravity(Gravity.CENTER, 0, 0); 
          toast.show(); 
         } 
        } 
       } 

       else 
       { 
        Toast toast=new Toast(getBaseContext()); 
        toast=Toast.makeText(Leave_form.this, "Please enter a valid date", Toast.LENGTH_LONG); 
        toast.setGravity(Gravity.CENTER, 0, 0); 
        toast.show(); 
       } 
      } 
     catch(Exception e) 
     { 
      Log.e("excepion",e.getMessage().toString()); 
     } 
     unregisterDateDisplay(); 
    } 
}; 

Dziękuję wszystkim za odpowiedzi. :)

0
package com.example.pickerdate; 

import java.util.Calendar; 

import android.app.Activity; 
import android.app.DatePickerDialog; 
import android.app.Dialog; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.DatePicker; 
import android.widget.DatePicker.OnDateChangedListener; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

    private TextView mDateDisplay; 
    private int mYear; 
    private int year1; 
    private int mMonth; 
    private int mDay; 
    private int month; 
    private int day; 
    static final int DATE_DIALOG_ID = 1; 
    Button pickDate; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mDateDisplay = (TextView) findViewById(R.id.dateDisplay); 
     pickDate = (Button) findViewById(R.id.pickDate); 
     pickDate.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       System.out.println("hello3"); 
       showDialog(DATE_DIALOG_ID); 

      } 
     }); 

     System.out.println("hello1"); 
     final Calendar c = Calendar.getInstance(); 
     mYear = c.get(Calendar.YEAR); 
     mMonth = c.get(Calendar.MONTH); 
     mDay = c.get(Calendar.DAY_OF_MONTH); 
     updateDisplay(); 
     year1 = mYear; 
     month = mMonth; 
     day = mDay; 
    } 

    @Override 
    protected Dialog onCreateDialog(int id) { 
     DatePickerDialog _date = null; 
     switch (id) { 

     case DATE_DIALOG_ID: 
      _date = new DatePickerDialog(this, mDateSetListener, mYear, mMonth, 
        mDay) { 
       @Override 
       public void onDateChanged(DatePicker view, int year, 
         int monthOfYear, int dayOfMonth) { 
        System.out.println("----------onDateChanged()-----------" 
          + mYear + "--" + year); 
        System.out.println("----------onDateChanged()-----------" 
          + mMonth + "--" + monthOfYear); 
        System.out.println("----------onDateChanged()-----------" 
          + mDay + "--" + dayOfMonth); 

        /* 
        * These lines of commented code used for only setting the 
        * maximum date on Date Picker.. 
        * 
        * if (year > mYear && year) view.updateDate(mYear, mMonth, 
        * mDay); 
        * 
        * if (monthOfYear > mMonth && year == mYear) 
        * view.updateDate(mYear, mMonth, mDay); 
        * 
        * if (dayOfMonth > mDay && year == mYear && monthOfYear == 
        * mMonth) view.updateDate(mYear, mMonth, mDay); 
        */ 

        // these below lines of code used for setting the maximum as 
        // well as minimum dates on Date Picker Dialog.. 

        if ((mYear > year) 
          || ((mMonth > monthOfYear) && (mYear == year)) 
          || ((mDay > dayOfMonth) && (mYear == year) && (mMonth == monthOfYear))) { 
         view.updateDate(year1, month, day); 

        } 

       } 
      }; 

     } 
     System.out.println("hello5"); 
     return _date; 
    } 

    protected void onPrepareDialog(int id, Dialog dialog) { 
     switch (id) { 

     case DATE_DIALOG_ID: 
      System.out.println("hello6"); 

      ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay); 
      break; 
     } 
    } 

    private void updateDisplay() { 
     System.out.println("hello2"); 
     mDateDisplay.setText(new StringBuilder() 
       // Month is 0 based so add 1 
       .append(mMonth + 1).append("-").append(mDay).append("-") 
       .append(mYear).append(" ")); 
    } 

    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() { 

     public void onDateSet(DatePicker view, int year, int monthOfYear, 
       int dayOfMonth) { 
      System.out.println("hello7"); 

      mYear = year; 
      mMonth = monthOfYear; 
      mDay = dayOfMonth; 
      System.out.println("year=" + year); 
      System.out.println("month=" + monthOfYear); 
      System.out.println("day=" + dayOfMonth); 
      updateDisplay(); 
     } 
    }; 
}