Android DatePicker in spinner mode not showing date

纵然是瞬间 提交于 2019-12-13 03:12:59

问题


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:

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.


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/51530726/android-datepicker-in-spinner-mode-not-showing-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!