2014-10-07 63 views
-3

我是新來的android 如何禁用日期選擇器中的未來日期,我嘗試了許多堆棧上的代碼,但它不能幫助任何人幫助我請求。如何禁用未來的日期選擇器在Android中

看一看代碼

public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.daty); 



       click=(ImageView)findViewById(R.id.click); 


       hdate=(TextView)findViewById(R.id.hdate); 

       timeStamp = new SimpleDateFormat("dd/MM/yyyy").format(new Date(System.currentTimeMillis())); 


       hdate.setText(timeStamp); 


       final Calendar myCalendar = Calendar.getInstance(); 

     // listener for date picker   
       final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() { 

        @Override 
        public void onDateSet(DatePicker view, int year, int monthOfYear, 
          int dayOfMonth) { 
         // TODO Auto-generated method stub 
         myCalendar.set(Calendar.YEAR, year); 
         myCalendar.set(Calendar.MONTH, monthOfYear); 
         myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth); 
         updateLabel(); 
        } 

        private void updateLabel() { 
         // TODO Auto-generated method stub 

         String myFormat = "dd/MM/yyyy"; 
         SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US); 
         hdate.setText(sdf.format(myCalendar.getTime())); 


        } 

       }; 

//texview listener 
       hdate.setOnClickListener(new OnClickListener() { 


        public void onClick(View v) { 
         // TODO Auto-generated method stub 

         new DatePickerDialog(Daty.this, date, myCalendar 
           .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), 
           myCalendar.get(Calendar.DAY_OF_MONTH)).show(); 


        } 


       }); 
+1

你嘗試用'datePickerDialog.getDatePicker()setMaxDate(System.currentTimeMillis的());'。 ? – 2014-10-07 06:37:25

+0

未來日期是指從當前日期開始的日期對嗎? – SathishKumar 2014-10-07 06:37:26

回答

6

您可以簡單地使用setMaxDate功能的日期選擇器。

DatePickerDialog datePickerDialog = new DatePickerDialog(getApplicationContext(), date, Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH); //date is dateSetListener as per your code in question 
datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis()); 

參考documentation

希望這有助於。

+1

非常感謝..它的作品..非常感謝你 – Aravind 2014-10-07 08:17:01

+0

有另一個疑問,是否有任何可能性日期選擇器從一個特定的月份和年開始 – Aravind 2014-10-07 08:26:39

+0

是的。 @Aravind只需在DatePickerDialog datePickerDialog = new DatePickerDialog(getApplicationContext(),date,Calendar.YEAR,Calendar.MONTH,Calendar.DAY_OF_MONTH)中按照您的要求傳遞月份的年份,月份和日期;' – 2014-10-07 08:27:32

-2
Use this : 
public class MainActivity extends Activity { 

    private TextView tvDisplayDate; 
    private DatePicker dpResult;  

    private int mYear; 
    private int mMonth; 
    private int mDay; 
    static final int DATE_DIALOG_ID = 1; 

//these variables are used to set max years need to set on Date Picker dialog... 
    int minYear1 = 2000; 
    int minMonth1 = 0;//0-january , 1-february , 2-march.. 
    int minDay1 =1; 

    int minYear = minYear1; 
    int minMonth = minMonth1; 
    int minDay = minDay1; 

    //these are the minimum dates to set Datepicker.. 
    private int year; 
    private int month; 
    private int day;  

    public String dateOutput=null; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     final Calendar c = Calendar.getInstance(); 
      mYear = c.get(Calendar.YEAR); 
      mMonth = c.get(Calendar.MONTH); 
      mDay = c.get(Calendar.DAY_OF_MONTH); 

      setCurrentDateOnView(); 

     Button pickDate = (Button) findViewById(R.id.btnChangeDate); 
     pickDate.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
         showDialog(DATE_DIALOG_ID); 
       } 
     }); 

    } 

    // display current date 
    public void setCurrentDateOnView() { 

     System.out.println("----------setCurrentDateOnView()-----------"); 

     tvDisplayDate = (TextView) findViewById(R.id.tvDate); 
     dpResult = (DatePicker) findViewById(R.id.dpResult); 

     final Calendar c = Calendar.getInstance(); 
     year = c.get(Calendar.YEAR); 
     month = c.get(Calendar.MONTH); 
     day = c.get(Calendar.DAY_OF_MONTH); 

     // set current date into textview 
     tvDisplayDate.setText(new StringBuilder() 
       // Month is 0 based, just add 1 
       .append(month + 1).append("-").append(day).append("-") 
       .append(year).append(" ")); 

     // set current date into datepicker 
     dpResult.init(year, month, day, null); 
    } 


    //this was the main part in program to Restrict the DatePicker Dialog to only its current date. 
    @Override 
    protected Dialog onCreateDialog(int id) { 
     System.out.println("----------onCreateDialog()-----------"); 
     DatePickerDialog _date = null; 

     switch (id) { 
     case DATE_DIALOG_ID: 
       _date = new DatePickerDialog(this, datePickerListener, 
         year, 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 (year < minYear) 
        view.updateDate(minYear, minMonth, minDay); 

        if (monthOfYear < minMonth && year == minYear ) 
        view.updateDate(minYear, minMonth, minDay); 



        if (dayOfMonth < minDay && year == minYear && monthOfYear == minMonth) 
        view.updateDate(minYear, minMonth, minDay); 


        if (year > mYear) 
        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); 
*/ 
        dateOutput = String.format("Date Selected: %02d/%02d/%04d", 
               dayOfMonth, monthOfYear+1, year); 
        // Log.d("Debug", dateOutput); 

        Toast.makeText(MainActivity.this,dateOutput, Toast.LENGTH_SHORT).show(); 
      } 
     };  
     } 
     return _date; 
    } 


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

     // when dialog box is closed, below method will be called. 
     public void onDateSet(DatePicker view, int selectedYear, 
       int selectedMonth, int selectedDay) {    

      System.out.println("----------onDateSet()-----------");         

      year = selectedYear; 
      month = selectedMonth; 
      day = selectedDay; 

      // set selected date into textview 
      tvDisplayDate.setText(new StringBuilder().append(month + 1).append("-").append(day).append("-").append(year).append(" ")); 

      // set selected date into datepicker also 
      dpResult.init(year, month, day, null); 
     } 
    }; 

} 
0

我在我的項目使用此代碼:

Calendar calendar = Calendar.getInstance(); 
          calendar.set(year,monthOfYear,dayOfMonth); 

          long selectedTimeInMillis = calendar.getTimeInMillis(); 
          long systemTimeInMillis = Calendar.getInstance().getTimeInMillis(); 
          if(selectedTimeInMillis<=systemTimeInMillis){ 
           setDate(year,monthOfYear,dayOfMonth); 
          }else{ 
           Toast.makeText(getContext(),"Cannot select future date",Toast.LENGTH_SHORT).show(); 
          } 
1
mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis()); 


ed_date.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       Calendar mcurrentDate=Calendar.getInstance(); 
       year=mcurrentDate.get(Calendar.YEAR); 
       month=mcurrentDate.get(Calendar.MONTH); 
       day=mcurrentDate.get(Calendar.DAY_OF_MONTH); 

       final DatePickerDialog mDatePicker =new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() 
       { 
        @Override 
        public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) 
        { 
           ed_date.setText(new StringBuilder().append(year).append("-").append(month+1).append("-").append(day)); 
          int month_k=selectedmonth+1; 

        } 
       },year, month, day); 
       mDatePicker.setTitle("Please select date"); 
       // TODO Hide Future Date Here 
       mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis()); 

       // TODO Hide Past Date Here 
       // mDatePicker.getDatePicker().setMinDate(System.currentTimeMillis()); 
       mDatePicker.show(); 
      } 
     }); 

其工作

相關問題