setMaxDate()在Lollipop 5.0.1 android上无法正常工作,需要适当的解决方案 [英] setMaxDate() not working perfectly on Lollipop 5.0.1 android and need proper solution
问题描述
即使将 setMaxDate()
应用于datepicker,我仍然能够在棒棒糖5.0.1上选择禁用日期。该代码对所有其他版本的android都适用,除了lollipop 5.0.1。
Even after applying setMaxDate()
to datepicker, I am still able to select the disabled dates on lollipop 5.0.1. The code is working fine for all other versions of android except for lollipop 5.0.1.
此处通过设置 setMaxDate()
,则没有用户应该能够选择禁用日期。如何为 DatePicker
以编程方式实现此目标?
Here after restricting the dates by setting setMaxDate()
, no user should be able to select disabled dates. How can I achieve this programmatically for DatePicker
?
我尝试了以下代码:-
datePickerDialog = new DatePickerDialog(myContext, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
}
}, mYear, mMonth, mDay);
datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis());
datePickerDialog.setCanceledOnTouchOutside(true);
datePickerDialog.setCancelable(true);
datePickerDialog.show();
我也尝试了以下解决方案,但这些似乎无效:-
Also I tried below solutions but these don't seem to work:-
datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis());
和
Calendar maxCal = Calendar.getInstance();
datePickerDialog.getDatePicker().setMaxDate(maxCal.getTimeInMillis());
和
Date maxDate = new Date();
datePickerDialog.getDatePicker().setMaxDate(maxDate.getTime());
请提供适用于棒棒糖的解决方案 setMaxDate()
。
Please provide solution which is working for lollipop to setMaxDate()
.
或者,如果您还有其他答案,请尝试提供官方报价,资源或链接(例如android开发者网站)(如果您知道详细信息) 。
Or if you have any other answer, please try to include official quote and resources or links (like android developer site) if you know with brief details. Thanks in advance.
推荐答案
是的,我发现了一个库 wdullaer / MaterialDateTimePicker
用于datePicker,它在 lollipop 5.0.1 中工作完美,就像在其他API版本中一样。
Yes, I have found one library wdullaer/MaterialDateTimePicker
for datePicker and it is working perfectly in lollipop 5.0.1 just like work in other API version.
解决方案库的链接为单击此处以获取MaterialDateTimePicker
通过在 dependencies {...} 的 build.gradle
( app)文件。
Add this library by adding below line in dependencies{...} of build.gradle
(app) file.
compile 'com.wdullaer:materialdatetimepicker:3.2.1'
然后根据需要实施以下代码:-
Then implement below code as per need:-
MainActivity mainActivity = (MainActivity) myContext;
final DatePickerDialog dpd = DatePickerDialog.newInstance(
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
//To do your task here
}
},
mYear,
mMonth,
mDay
);
dpd.setVersion(DatePickerDialog.Version.VERSION_2);
dpd.setMaxDate(Calendar.getInstance());
dpd.show(mainActivity.getFragmentManager(), "Datepickerdialog");
这篇关于setMaxDate()在Lollipop 5.0.1 android上无法正常工作,需要适当的解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!