Android DatePicker in spinner mode not showing dat

2019-08-27 16:23发布

I'm using DatePicker in spinner mode, and sets min and max limit. Then updates the date as max value. Date column does not have a value when the DatePickerDialog shown. When I tap/scroll up or down the values are shown.

MyLayout:

         <DatePicker
                android:id="@+id/date_picker"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="@dimen/spacing_default"
                android:layout_marginRight="@dimen/spacing_default"
                android:datePickerMode="spinner"
                android:calendarViewShown="false" />

Java Part : (in onViewCreated)

 private static final int DATE_PICKER_MONTH_OFFSET = 1;
 private static final int DATE_PICKER_MIN_DATE_OFFSET_IN_MIN = 1;

datePicker.setMinDate( now().minusMinutes( 
         DATE_PICKER_MIN_DATE_OFFSET_IN_MIN).getMillis() );
datePicker.setMaxDate( activationDateTime.getMillis() );
datePicker.updateDate( activationDateTime.getYear(), 
         activationDateTime.getMonthOfYear() - DATE_PICKER_MONTH_OFFSET,
                    activationDateTime.getDayOfMonth() );

Problematic Screenshot:

enter image description here

What more is needed to make the date part to be displayed when the DatePickerDialog is shown?

UPDATE/EDIT:

The seems that picker show an empty view when the value set is same as the max limit. I could resolve the issue (while launching the DatePicker) by placing datePicker.updateDate first and then call datePicker.setMaxDate. The setMaxDate will validate the previously value and sets correctly. But now I face the same problem when I scroll the month from Jul to Aug, the day view becomes empty!

Any solution/suggestion appreciated.

2条回答
2楼-- · 2019-08-27 16:48

There seems to have bug in the android widget DatePicker in the spinner mode. Already such a ticket present in the Google issue tracker also : https://issuetracker.google.com/issues/64508670

Unfortunately no work around or suggestions available in the issue tracker.

查看更多
We Are One
3楼-- · 2019-08-27 16:52

Had the same issue. Solved it like that:

 datePicker.init(initYear, initMonthOfYear, initDayOfMonth,
            new DatePicker.OnDateChangedListener() {
                @Override
                public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    Resources res = Resources.getSystem();
                    int id = res.getIdentifier("day", "id", "android");
                    NumberPicker dayPicker = datePicker.getRootView().findViewById(id);
                    if (dayPicker != null) {
                        id = res.getIdentifier("numberpicker_input", "id", "android");
                        TextView inputText = dayPicker.findViewById(id);
                        if (inputText != null && TextUtils.isEmpty(inputText.getText())) {
                            inputText.setText(String.valueOf(dayPicker.getValue()));
                        }
                    }
                }
            });
查看更多
登录 后发表回答