2010-09-14 145 views
30

我正在寫我的第一個應用程序,並且我有一個關於DatePicker的問題。Android日曆查看日期選取器

我的應用需要用戶輸入日期。最人性化的方式將彈出窗口,顯示當前月份像日曆網格日曆般的小部件 - 是這樣的:

enter image description here

我想使用替代DatePicker接口 - 其中有月份,日期和年份字段,每個字段都有一個向上和向下按鈕來增加/減少。

這種類型的功能內置於任何Android小部件或視圖中,還是必須設計自己的自定義組件來執行此操作?我想這已經存在了,看看這種類型的UI在非移動應用程序和網頁中經常使用多少。

謝謝!

+0

您能否將此任務所選答案更新至此任務離子?如果他們沒有看到所有的答案,人們可​​能會誤導他人。正如其他人的迴應所述,API級別11+中有一個CalendarView。謝謝! – vosmith 2014-02-26 13:31:30

回答

11

這種類型的功能是否內置到任何android小部件或視圖中,還是必須設計我自己的自定義>組件來執行此操作?

在Android SDK中沒有組件,對不起。您說明的小部件對觸摸屏來說太小了。您可以實現更大的功能(請參閱日曆應用程序),但您主要依靠自己的功能。

+1

你能指出我的日曆應用程序代碼嗎? – 2012-02-16 18:05:11

+8

你可以提供日曆應用程序的鏈接,你在你的答案中提到.. – Nishant 2012-04-25 09:52:12

4

發現在http://caughtinthemobileweb.wordpress.com/2011/06/20/how-to-implement-calendarview-in-android/


良好的implemetation此外,由於API級別11(安卓3.0)有一直是C的本地實現alendarView http://developer.android.com/reference/android/widget/CalendarView.html

+0

+1提到本地日期選擇器。還有DatePickerDialog:http://developer.android.com/reference/android/app/DatePickerDialog.html – 2012-06-20 23:22:04

+0

這裏是偉大的重寫沒有網格視圖︰https://github.com/AtRow/CalendarView – Cfr 2013-03-05 11:42:06

17

現在,在2014年,即使是本地DatePicker (link)含有少量全息尋找CalendarView (link)每月選擇一天。

,您可以選擇,如果兩個紡紗和CalendarView或只是其中之一是通過設置顯示:

  • android:calendarViewShown
  • android:spinnersShown

我不知道,如果它只是API級別16+或者甚至是冰淇淋三明治,但它在那裏。這是它的外觀默認:

enter image description here


此外,在API級別21和更高的有新材料主題DatePicker,看起來像以下:

enter image description here

這是API 21+上的默認設置,並且不再有旋鈕,但可以通過設置

android:datePickerMode="spinner" 

在您的XML中。如果你想使用它像彈出寫上你的onclick https://github.com/truefedex/android-date-picker enter image description here

+1

試過了有趣的ICS,看起來像holo – Zharf 2014-08-12 12:47:40

+0

@anoniim,謝謝datePickerMode提示! – user3855005 2015-02-23 09:01:20

1

嘗試使用這個組件

if (calendarPopup == null) { 
    calendarPopup = new PopupWindow(getContext()); 
    CalendarPickerView calendarView = new CalendarPickerView(getContext()); 
    CalendarNumbersView calendar = (CalendarNumbersView) calendarView.findViewById(com.phlox.datepick.R.id.calendar); 
    calendar.setShowDayNames(false); 
    calendarView.setListener(onDateSelectionListener); 
    calendarPopup.setContentView(calendarView); 
    calendarPopup.setWindowLayoutMode(
     MeasureSpec.makeMeasureSpec(llCalendar.getWidth(), MeasureSpec.EXACTLY), 
     ViewGroup.LayoutParams.WRAP_CONTENT); 
    calendarPopup.setHeight(1); 
    calendarPopup.setWidth(llCalendar.getWidth()); 
    calendarPopup.setOutsideTouchable(true); 
} 
calendarPopup.showAsDropDown(llCalendar);