2013-07-26 192 views
9

我想在DatePickerDialog。我今天的日期之前禁用日期是在Android的新的請建議我如何能做到這一點。這裏是我的代碼,我有DatePickerDialog如何在DatePickerDialog Android的今日日期之前禁用日期?

書面
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(val); 
      } 
     }; 
     depart.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        new DatePickerDialog(this, date, myCalendar 
          .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), 
          myCalendar.get(Calendar.DAY_OF_MONTH)).show(); 
        val=1; 
       } 
      }); 

     returnDate.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        new DatePickerDialog(this, date, myCalendar 
          .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), 
          myCalendar.get(Calendar.DAY_OF_MONTH)).show(); 
          val=2; 
       } 
      }); 


private void updateLabel(int val) { 

     String myFormat = "dd/MM/yy"; //In which you need put here 
     SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US); 
     Log.d("Date vlue ", "==="+sdf.format(myCalendar.getTime())); 
     if(val==1) 
      depart.setText(sdf.format(myCalendar.getTime())); 
     else 
      returnDate.setText(sdf.format(myCalendar.getTime())); 

     } 

請建議我什麼必須做

+0

http://stackoverflow.com/questions/4625002/android-disable-past-dates-in-datepicker –

回答

8

看到這個例子..!

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.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.DatePicker; 
import android.widget.TextView; 

public class MyAndroidAppActivity extends Activity { 

    private TextView tvDisplayDate; 

    private Button btnChangeDate; 

    private int myear; 
    private int mmonth; 
    private int mday; 

    static final int DATE_DIALOG_ID = 999; 

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

     setCurrentDateOnView(); 
     addListenerOnButton(); 

    } 

    // display current date 
    public void setCurrentDateOnView() { 

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

     final Calendar c = Calendar.getInstance(); 
     myear = c.get(Calendar.YEAR); 
     mmonth = c.get(Calendar.MONTH); 
     mday = c.get(Calendar.DAY_OF_MONTH); 

     // set current date into textview 
     tvDisplayDate.setText(new StringBuilder() 
       // Month is 0 based, just add 1 
       .append(mmonth + 1).append("-").append(mday).append("-") 
       .append(myear).append(" ")); 
    } 

    public void addListenerOnButton() { 

     btnChangeDate = (Button) findViewById(R.id.btnChangeDate); 

     btnChangeDate.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       showDialog(DATE_DIALOG_ID); 

      } 

     }); 

    } 

    @Override 
    protected Dialog onCreateDialog(int id) { 
     switch (id) { 
     case DATE_DIALOG_ID: 
      // set date picker as current date 
      DatePickerDialog _date = new DatePickerDialog(this, datePickerListener, myear,mmonth, 
        mday){ 
       @Override 
       public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) 
       { 
        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); 

       } 
      }; 
      return _date; 
     } 
     return null; 
    } 

    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) { 
      myear = selectedYear; 
      mmonth = selectedMonth; 
      mday = selectedDay; 

      // set selected date into textview 
      tvDisplayDate.setText(new StringBuilder().append(mmonth + 1) 
        .append("-").append(mday).append("-").append(myear) 
        .append(" "));  

     } 
    }; 

} 

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/btnChangeDate" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Change Date" /> 

    <TextView 
     android:id="@+id/lblDate" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Current Date (M-D-YYYY): " 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/tvDate" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

</LinearLayout> 
+0

這是關於什麼? – Enrichman

+0

在DatePickerDialog的今天日期之前禁用日期。 – Hariharan

+0

我試過這個,它只允許在今天的日期前挑選日期 – Developer

19

使用getDatePicker()方法獲取DatePickerDialog中使用的DatePicker並使用setMinDate(Long millis)方法。

傳遞給它你必須設置的最小日期(從Epoch以毫秒爲單位)。

所以,你可以這樣做

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); 
Date d = sdf.parse("21/12/2012"); 
DatePicker datePicker = date.getDatePicker(); 
datePicker.setMinDate(d.getTime()); 

編輯:

好了,所以當你創建你DatePickerDialog,顯示它之前,只是把它保存到一個變量,設置最短日期然後顯示它。

DatePickerDialog dpd = new DatePickerDialog(IweenFlightSearch.this, date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)); 
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); 
Date d = sdf.parse("21/12/2012"); 
dpd.getDatePicker().setMinDate(d.getTime()); 
dpd.show(); 

這應該工作。

+0

我沒有得到你必須做出這些修改,以便今天之前的日期將被禁用 – Developer

+0

你還沒有發佈你的所有代碼,所以你很難說「你在哪裏」進行修改。你應該有一個DatePickerDialog或DatePicker的地方。如果你想獲得進一步的幫助,你應該告訴我們你在哪裏使用這些課程。 – Enrichman

+0

請在我的問題中看到我已發佈完整的代碼,我正在使用datePickerDailog – Developer

2

我得到了解決。希望這將有助於有人 靜態最終詮釋DATE_DIALOG_ID = 999;

depart.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        showDialog(DATE_DIALOG_ID); 

       } 
      }); 



protected Dialog onCreateDialog(int id) { 
     final Calendar now = Calendar.getInstance(); 

     switch (id) { 
     case DATE_DIALOG_ID: 
      // set date picker as current date 
      DatePickerDialog _date = new DatePickerDialog(this, date,myCalendar 
        .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), 
        myCalendar.get(Calendar.DAY_OF_MONTH)){ 
       @Override 
       public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) 
       { 
        if (year < now.get(Calendar.YEAR)) 

         view.updateDate(myCalendar 
           .get(Calendar.YEAR), myCalendar 
           .get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)); 

        if (monthOfYear < now.get(Calendar.MONTH) && year == now.get(Calendar.YEAR)) 
         view.updateDate(myCalendar 
           .get(Calendar.YEAR), myCalendar 
           .get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)); 

        if (dayOfMonth < now.get(Calendar.DAY_OF_MONTH) && year == now.get(Calendar.YEAR) && 
          monthOfYear == now.get(Calendar.MONTH)) 
         view.updateDate(myCalendar 
           .get(Calendar.YEAR), myCalendar 
           .get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)); 
         } 
      }; 
      return _date; 
     } 
     return null; 
    } 
3

我知道這太晚了。但希望這會有所幫助。使用

date .getDatePicker().setMinDate(System.currentTimeMillis() - 1000); 

你的情況的日期是DatePickerDialog

乾杯!

+1

你能否解釋一下減去1000的需要 – Arunkrishna

+0

它完美的工作,謝謝。可能-1000是因爲你得到當前時間毫秒 – Kzaf

+1

最有可能是因爲這個IllegalArgumentException問題。 https://stackoverflow.com/questions/13661788/how-to-set-minimum-datepicker-date-to-current-date – Artemiy

3

//創建DatePickerDialog的新實例,並返回它

DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), this,year,month,day); 
      datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis()); 
      return datePickerDialog; 
相關問題