2011-06-23 36 views
16

我需要能夠在我的應用程序中選擇日期和時間 它應該通過使用對話框或開始新的意圖來完成。如何在DatePicker中設置當前日期?

我現在做的方式是,我開始了一個新的意圖,因爲我需要設置日期和時間。

我的問題是,我需要設置在Datepicker當前日期(這是可能的使用setCurrentHoursetCurrentMinuteTimepicker)的onCreate,並設置爲下限。

我無法在Android API中找到與DatePicker這樣的功能。

任何人有一個建議來解決這個問題?

在此先感謝!

編輯: 要點:DatePickerTimePicker必須在同一個對話框/意圖

+0

以下是如何設置當前日期並設置最小和最大可選日期:http://stackoverflow.com/questions/6421874/how-to-get-the-date-set-in-在datepicker-widget-in-android/7961268#7961268 –

回答

55

直代碼

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

DatePickerDialog dialog = 
    new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay); 
dialog.show(); 
+0

看起來不錯,但我需要時間在同一個對話框中 – Ikky

+0

然後總是使用這些對話框:) –

+0

謝謝爲我工作 –

0

的DatePicker的教程似乎做正是這一點?

http://developer.android.com/resources/tutorials/views/hello-datepicker.html

參考:

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

    // display the current date (this method is below) 
    updateDisplay(); 
+0

不,我想在datePicker上設置日期,而不是textview – Ikky

0

檢查this有圖片證明。

+0

我還需要在同一個對話框或intent中設置時間 – Ikky

1

您可以通過Calendar類查詢當前日期。

Calendar now = Calendar.getInstance(); 
now.get(Calendar.WHATDOYOUNEED); 

給它的get()方法與不同的參數來定製您的查詢。

17

試試這個:

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 datepicker 
datepicker.init(year, month, day, null); 
8

如果你只是尋找的setCurrentHour()setCurrentMinute()TimePicker等價,但DatePicker - 見updateDate (int year, int month, int dayOfMonth)DatePicker

DatePicker mDatePicker = (DatePicker) findViewById(R.id.datePicker); 
mDatePicker.updateDate(1994, 6, 12); 
// Sets date displayed on DatePicker to 12th June 1994 
相關問題