我在Android小部件的新DatePickerDialog上遇到一个奇怪的视觉错误。我需要将对话框的可选日期范围设置为1个月,但不应同时选择将来的日期。逻辑按预期工作,但视觉效果有某种错误。这是它的外观:
请注意,“ 5”已禁用,我无法选择它。但是,它看起来是可选的。我应该怎么做才能解决这个问题?任何帮助表示赞赏,谢谢。 (今天的今天也是5点,我认为这个错误来自那里。)
编辑:这是创建对话框的方式。
@SuppressLint("ValidFragment")
public static class DateDialog extends DialogFragment implements DatePickerDialog.OnDateSetListener
{
private static final long MONTH = 2592000000L;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
// Use previous selected date in the date picker
final Calendar c = Calendar.getInstance();
c.setTimeInMillis(DateUtils.getAtTimeTimestamp(startDate));
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getActivity(), R.style.AppTheme_DialogTheme, this, year, month, day);
Date date = new Date(); // get current time
// get target maximum date which can be 1 month above
long targetDateTime = DateUtils.getAtTimeTimestamp(startDate) + MONTH;
if (date.getTime() < targetDateTime)
dialog.getDatePicker().setMaxDate(date.getTime()); // now because it can't be future
else
dialog.getDatePicker().setMaxDate(targetDateTime); // should also be one month above from the start date.
dialog.getDatePicker().setMinDate(DateUtils.getAtTimeTimestamp(startDate));
return dialog;
}
@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2)
{
// logic between date selections
}
}
DateUtils.getAtTimeTimestamp(String date)基本上将字符串转换为以毫秒为单位的长整数。