How to set datePickerMode=“spinner” programmatically?

强颜欢笑 提交于 2019-12-21 09:15:26

问题


I need create DatePicker programmatically. And i can't use xml layout. i know set spinner in xml datePickerMode="spinner" and then i can use datePicker.setSpinnersShown(true); and datePicker.setCalendarViewShown(false) I try add AttributeSet into constructor but i don't know how use it. Can someone help? How to create a DataPicker in spinner mode?

Thank you


回答1:


I've already found a post, which it would describe you this problem:

I have found a the explanation in the following post (which describes a problem very similar to mine) :

Android Material Design Inline Datepicker issue

In fact the setCalendarViewShown(false) and setSpinnersShown(true) are apparently not working anymore in latest versions.

We have to use an explicit XML attribute like this one android:datePickerMode="spinner".

The problem is that I'm using a DialogFragment without any XML layout (just a date picker dialog). So I cannot set any XML attribute.

The solution is to create a dedicated custom dialog with an XML layout file using the requested attribute.

FROM: Impossible to make my DatePickerDialog use a spinner style programmatically

Hope it help




回答2:


If you want to set it programmatically you can you the below code but I haven't checked this code whether its working or not. Let me know if I can help you in any other way.

datepickerDialog_object.getDatePicker().setSpinnersShown(true);
datepickerDialog_object.getDatePicker().setCalendarViewShown(false);



回答3:


It works for me to set the DatePickerMode spinner mode programmatically.

Theme_Holo_Dialog_MinWidth is deprecated

 val dpDialog = DatePickerDialog(activity!!,
            android.R.style.Theme_Holo_Dialog_MinWidth,listener,
            year, month, day)
        dpDialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        dpDialog.show()


来源:https://stackoverflow.com/questions/34723495/how-to-set-datepickermode-spinner-programmatically

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