日期选择器的Android日历视图

时间:2010-09-14 20:38:23

标签: android calendar android-datepicker

我正在编写我的第一个应用,我对DatePicker提出了一个问题。

我的应用要求用户输入日期。最用户友好的方式是弹出一个类似日历的小部件,像日历网格一样显示当前月份 - 如下所示:

enter image description here

我想用它代替DatePicker接口 - 它有月,日和年字段,每个字段都有一个向上和向下按钮来递增/递减。

这种功能是否内置于任何Android小部件或视图中,或者我是否必须设计自己的自定义组件才能执行此操作?我认为这已经存在,看看在非移动应用和网页中频繁使用这种类型的UI的程度。

谢谢!

8 个答案:

答案 0 :(得分:17)

现在,在2014年,即使是原生的DatePicker (link)包含小型Holo,也可以查看CalendarView (link)每月选择的一天。

如果通过设置显示两个微调器和CalendarView或只显示其中一个,则可以选择:

  • android:calendarViewShown
  • android:spinnersShown

我不确定它是否只是API级别16+或者它是否甚至在冰淇淋三明治中,但它就在那里。这是默认情况下的外观:

enter image description here


此外,在API级别21及更高版本上,有一个新的主题DatePicker,如下所示:

enter image description here

这是API 21+的默认值,并且不再有旋转器,但您可以通过设置

切换回Holo
android:datePickerMode="spinner"

在您的XML中。

答案 1 :(得分:12)

由于API 11本身存在:CalendarView

enter image description here

自API 7以来,此视图位于HoloEverywhere

答案 2 :(得分:11)

  

这种类型的功能内置于任何Android小部件或视图中,还是我必须设计自己的自定义>组件来做这个?

Android SDK中没有组件,抱歉。您说明的小部件对于触摸屏来说太小了。你可以实现更大的东西(参见日历应用程序),但你很大程度上依靠自己。

答案 3 :(得分:7)

答案 4 :(得分:4)

我最近把它写成模块化应用程序。以下是一些sample codedocumentation screenshots.apk download

答案 5 :(得分:4)

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

中找到了一个很好的实现

此外,自API级别11(Android 3.0)起,日历视图http://developer.android.com/reference/android/widget/CalendarView.html

的本机实现

答案 6 :(得分:1)

尝试使用此组件: https://github.com/truefedex/android-date-picker enter image description here

如果您想在onc​​lick上使用弹出式写法:

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);

答案 7 :(得分:0)

您可以使用此库进行日期选择

https://github.com/square/android-times-square/