日期选择器的 Android 日历视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3712710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:29:50  来源:igfitidea点击:

Android Calendar View for Date Picker

androidcalendarandroid-datepicker

提问by ndtrek07

I'm writing my first app, and I have a question about DatePicker.

我正在编写我的第一个应用程序,我有一个关于DatePicker.

My app requires the user to input a date. The most user-friendly way would be to popup a calendar-like widget that displays the current month like a calendar grid - something like this:

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

enter image description here

在此处输入图片说明

I want to use that in place of the DatePickerinterface - which has Month, Day, and Year fields, each with an up and down button to increment/decrement.

我想用它代替DatePicker界面——它有月、日和年字段,每个字段都有一个向上和向下按钮来增加/减少。

Is this type of functionality built into any Android widget or view, or would I have to design my own custom component to do this? I figured this would already exist, seeing how much this type of UI is used so frequently in non-mobile apps and web pages.

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

Thanks!

谢谢!

采纳答案by CommonsWare

Is this type of functionality built into any android widget or view, or would I have to design my own custom > component to do this?

这种类型的功能是否内置在任何 android 小部件或视图中,或者我是否必须设计自己的自定义 > 组件来执行此操作?

There is no component for that in the Android SDK, sorry. The widget you illustrate is too small for a touchscreen. You can implement something larger (see the Calendar app), but you are largely on your own for that.

抱歉,Android SDK 中没有相关组件。您说明的小部件对于触摸屏来说太小了。您可以实现更大的功能(请参阅日历应用程序),但您在很大程度上要靠自己来实现。

回答by Marcel Bro

Now, in 2014, even the native DatePicker(link)contains small Holo looking CalendarView(link)to pick a day in month.

现在,在 2014 年,即使是本机DatePicker(链接)也包含小 Holo 外观CalendarView(链接)来选择一个月中的一天。

You can choose, if both spinners and CalendarViewor just one of them is displayed by setting:

您可以选择,是否CalendarView通过设置显示两个微调器或仅显示其中之一:

  • android:calendarViewShown
  • android:spinnersShown
  • android:calendarViewShown
  • android:spinnersShown

I'm not sure if it's just API level 16+ or if it was even in Ice Cream Sandwich, but it's there. This is how it looks by default:

我不确定它是否只是 API 级别 16+ 或者它是否甚至在 Ice Cream Sandwich 中,但它就在那里。这是默认情况下的外观:

enter image description here

在此处输入图片说明



Moreover, on API level 21 and higher there is a new Material themed DatePickerthat looks like following:

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

enter image description here

在此处输入图片说明

This is default on API 21+ and there are no spinners anymore, but you can switch back to the Holo one by setting

这是 API 21+ 上的默认设置,不再有微调器,但您可以通过设置切换回 Holo 一个

android:datePickerMode="spinner"

in your XML.

在您的 XML 中。

回答by Brais Gabin

Since the API 11 there natively: CalendarView

由于 API 11 本身就有:CalendarView

enter image description here

在此处输入图片说明

This View is in HoloEverywheresince API 7.

自 API 7 起,此视图位于HoloEverywhere 中

回答by Gaurav Vashisth

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

http://caughtinthemobileweb.wordpress.com/2011/06/20/how-to-implement-calendarview-in-android/ 中找到了一个很好的实现



Also Since API level 11 (Android 3.0) there has been the native implementation of the CalendarView http://developer.android.com/reference/android/widget/CalendarView.html

此外,从 API 级别 11(Android 3.0)开始,CalendarView http://developer.android.com/reference/android/widget/CalendarView.html有了本机实现

回答by kostmo

I have recently written exactly this as a modular app. Here is some sample code, documentationwith screenshots, and .apk download.

我最近把它写成一个模块化的应用程序。下面是一些示例代码、带有屏幕截图的文档.apk 下载

回答by Fedir Tsapana

Try to use this component: https://github.com/truefedex/android-date-pickerenter image description here

尝试使用这个组件:https: //github.com/truefedex/android-date-picker在此处输入图片说明

If you want to use it like popup write on your onclick:

如果你想像弹窗一样使用它,在你的 onclick 上写:

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

回答by Chayon Ahmed

you can use this lib for date selection

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

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

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