DatePickerDialog如何不显示日历只是一个微调器?

时间:2017-08-03 20:01:18

标签: android android-datepicker

我想创建一个像这样的DatePickerDialog:

mDatePickerDialog = new DatePickerDialog(context, null /* listener */, 2000/* year */, 1/* month */, 1/* dayOfMonth */);

但是像这样的微调器:

enter image description here

我尝试在xml中设置它:

  <style name="MyDatePicker" parent="@android:style/Widget.DatePicker">
    <item name="android:calendarViewShown">false</item>
    <item name="android:datePickerMode">spinner</item>
  </style>

  <style name="MyAppTheme" parent="@android:style/Theme.Material.Light.NoActionBar"> 
    <item name="android:datePickerStyle">@style/MyDatePicker</item>
  </style>

但没有奏效:(

2 个答案:

答案 0 :(得分:1)

尝试使用此对话框:

DatePickerDialog dialogDatePicker = new DatePickerDialog(getActivity(), this, year, month, day);
            dialogDatePicker.getDatePicker().setCalendarViewShown(false);
            return dialogDatePicker;

更新:

setCalendarViewShown(false);被删除,所以你需要使用xml将datePickerMode设置为spinner,如下所示:

<DatePicker
    ...
    android:datePickerMode="spinner" />

答案 1 :(得分:0)

我使用此样式来获取微调器日期选择器:

<style name="MySpinnerDatePickerStyle" parent="android:Theme.Material.Dialog">
    <item name="android:datePickerStyle">@style/MySpinnerDatePicker</item>
</style>

<style name="MySpinnerDatePicker" parent="android:Widget.Material.DatePicker">
    <item name="android:datePickerMode">spinner</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="android:textColorPrimary">@color/white</item>
</style>