2015-03-08 131 views
0

我知道Android CalendarView是人类历史上最繁忙的软件之一,但我必须使用它,所以才来这里询问。Android CalendarView:无法设置最短日期和所选日期

我需要打开一个CalendarView有:

  • 最小日期设置为当天;
  • 选定日期设置为今天。

这是我要做的事:

int day = 8; 
int month = 2; 
int year = 2015; 

Calendar calendar = Calendar.getInstance(); 
calendar.set(year, month, day); 

CalendarView calendarView = (CalendarView) findViewById(R.id.calendarView); 
calendarView.setMinDate(calendar.getTimeInMillis()-2000); 

calendarView.setDate(calendar.getTimeInMillis(), true, false); 
fixIncredibleBugOfCalendarView(calendarView, calendar); 
calendarView.setOnDateChangeListener(this); 

当我运行上面的代码中,CalendarView

  • 显示月9日为第一选择的一天,而不是第八名(DAY_OF_MONTH从1开始);

  • 显示3月15日为选定日期;

所以我geniously决定包括标有*的两条线:

int day = 8; 
int month = 2; 
int year = 2015; 

Calendar calendar = Calendar.getInstance(); 
calendar.set(year, month, day); 

CalendarView calendarView = (CalendarView) findViewById(R.id.calendarView); 
calendar.add(Calendar.DAY_OF_MONTH, -1); // * - subtract one day, i.e. March 7 
calendarView.setMinDate(calendar.getTimeInMillis()); 

calendar.add(Calendar.DAY_OF_MONTH, 1); // * add one day, back to 8 
calendarView.setDate(calendar.getTimeInMillis(), true, false); 
fixIncredibleBugOfCalendarView(calendarView, calendar); 
calendarView.setOnDateChangeListener(this); 

在第一行中我删除某一天,在第二个我想补充一个。

有了这两个行,我CalendarView显示:

  • 月7日,作为第一个可选择的日期;
  • 3月15日为选定日期。

的方法fixIncredibleBugOfCalendarView(...)应该解决的东西(我发现它SO):

private void fixIncredibleBugOfCalendarView(CalendarView cal, Calendar date) { 
     // Workaround for CalendarView bug relating to setMinDate(): 
     // https://code.google.com/p/android/issues/detail?id=42750 
     // Set then reset the date on the calendar so that it properly 
     // shows today's date. The choice of 24 months is arbitrary. 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
      date.add(Calendar.MONTH, 24); 
      cal.setDate(date.getTimeInMillis(), false, true); 
      date.add(Calendar.MONTH, -24); 
      cal.setDate(date.getTimeInMillis(), false, true); 
     } 
    } 

这不应该是这么辛苦,我不认为这是一个错误:它更可能是我的错。两个问题:请你如此友善地告诉我如何:

  • 告诉CalendarView它应该显示的最短日期?
  • have CalendarView显示为selected我决定选择的日期是什么?

干杯

+0

如果CalendarView是越野车,为什么不使用[替代](https://github.com/square/android-times-square)?还有一些其他的选择。 – JonasCz 2015-03-08 18:51:57

+0

谢谢。由于我在此花了一个下午,我想看看是否有人能够解决问题:这是一项非常简单的任务。否则,我会使用替代品(很难相信Android开发人员制作的简单选择器太麻烦了,而替代品则不是)。 – 2015-03-08 18:55:28

+0

也许试试[这个答案](http://stackoverflow.com/a/20683328/4428462),可能与您的'fixIncredibleBugOfCalendarView'方法? – JonasCz 2015-03-08 18:58:18

回答

0

这是晚了,大概你已经解决了的事情。但这里是我的代码来解决它

android.icu.util.Calendar c = android.icu.util.Calendar.getInstance(); 

    Long min = c.getTime().getTime(); 
    Long max = 2629746000L + c.getTime().getTime(); 


    CalendarView cv = (CalendarView) findViewById(R.id.date_picker); 
    cv.setMinDate(min); 
    cv.setMaxDate(max); 

你也可以CalenderView只需要在长格式的思想。你也可以去java

Data d = new Date(); 
d.getTime(); 

但不知何故,它不工作,因为目前的时区。 相反,日历返回与当前时区设置相关的dateObject。

相关问题