android5.0日历范围

来源:互联网 发布:椰子油 知乎 编辑:程序博客网 时间:2024/06/05 17:14

前言:最近项目攻关,一大堆乱七八糟不归我管的模块bug铺天盖地地飞过来,实在是浪费时间,为了解bug而解bug,根本不清楚个中的原理,到头来什么也学不到。
就像这个bug,原理完全不懂,查网上资料根本查不到,只能自己先半懂不懂地记录下来,以后遇到了再细细研究、更正。

android5.0系统的时间范围大致在1970年1月1日~2037年12月31日,具体原因大概和寄存器的位数只能存这么些,6.0之后扩展到2100年了。(我用我的华为手机7.0发现竟然超过了2100年,实在是不懂了……)

DatePicker.java(/frameworks/base/core/java/android/widget)int startYear = attributesArray.getInt(R.styleable.DatePicker_startYear,                    DEFAULT_START_YEAR);int endYear = attributesArray.getInt(R.styleable.DatePicker_endYear, DEFAULT_END_YEAR);private static final int DEFAULT_START_YEAR = 1900;private static final int DEFAULT_END_YEAR = 2100;

从默认值来看,android确实是可以支持1900~2100的,也有可能是高通自己改的。

由于没有定义两个变量,所以使用默认值,那如果要自己定义范围呢?

//声明attrs.xml(/frameworks/base/core/res/res/values)<declare-styleable name="DatePicker">        <!-- The first year (inclusive), for example "1940".             {@deprecated Use minDate instead.} -->        <attr name="startYear" format="integer" />        <!-- The last year (inclusive), for example "2010".             {@deprecated Use maxDate instead.} -->        <attr name="endYear" format="integer" />        <!-- Whether the spinners are shown. -->        <attr name="spinnersShown" format="boolean" />        <!-- Whether the calendar view is shown. -->        <attr name="calendarViewShown" format="boolean" />        <!-- The minimal date shown by this calendar view in mm/dd/yyyy format. -->        <attr name="minDate" format="string" />        <!-- The maximal date shown by this calendar view in mm/dd/yyyy format. -->        <attr name="maxDate" format="string" />        <!-- The first day of week according to {@link java.util.Calendar}. -->        <attr name="firstDayOfWeek" />        <!-- @hide The layout of the date picker. -->        <attr name="internalLayout" format="reference"  />        <!-- @hide The layout of the legacy DatePicker. -->        <attr name="legacyLayout" />        <!-- The background color for the date selector 's day of week. -->        <attr name="dayOfWeekBackground" format="color|reference" />        <!-- The text color for the date selector's day of week. -->        <attr name="dayOfWeekTextAppearance" format="reference" />        <!-- The month's text appearance in the date selector. -->        <attr name="headerMonthTextAppearance" format="reference" />        <!-- The day of month's text appearance in the date selector. -->        <attr name="headerDayOfMonthTextAppearance" format="reference" />        <!-- The year's text appearance in the date selector. -->        <attr name="headerYearTextAppearance" format="reference" />        <!-- The background for the date selector. -->        <attr name="headerBackground" />        <!-- @hide The selected text color for the date selector. Used as a             backup if the text appearance does not explicitly have a color             set for the selected state. -->        <attr name="headerSelectedTextColor" />        <!-- The list year's text appearance in the list. -->        <attr name="yearListItemTextAppearance" format="reference" />        <!-- The list year's selected circle color in the list. -->        <attr name="yearListSelectorColor" format="color" />        <!-- The text color list of the calendar. -->        <attr name="calendarTextColor" format="color" />        <!-- @hide The selected text color for the calendar. Used as a backup             if the text color does not explicitly have a color set for the             selected state. -->        <attr name="calendarSelectedTextColor" format="color" />        <!-- Defines the look of the widget. Prior to the L release, the only choice was             spinner. As of L, with the Material theme selected, the default layout is calendar,             but this attribute can be used to force spinner to be used instead. -->        <attr name="datePickerMode">            <!-- Date picker with spinner controls to select the date. -->            <enum name="spinner" value="1" />            <!-- Date picker with calendar to select the date. -->            <enum name="calendar" value="2" />        </attr></declare-styleable>//定义styles.xml(/frameworks/base/core/res/res/values)<style name="Widget.DatePicker">        <item name="datePickerMode">spinner</item>        <item name="legacyLayout">@layout/date_picker_legacy</item>        <item name="calendarViewShown">false</item>        <item name="startYear">1970</item>        <item name="endYear">2037</item></style>

注意项目overlay。
应用中计算月份和天数的方法都需要添加范围来约束。
解决应用bug后,别忘记系统设置还有一个“日期和时间”选项,这个坑可是被牵连出来的,人家可是按照系统的范围设置成1970.1.1~2037.12.31的哦,我们硬该为2036……
在关闭自动确定日期和时间,手动设置日期超过2036时,我们的应用就会出现各种乱七八糟的问题,所以需要改变设置日期的范围:

DateTimeSettings.java(/packages/apps/Settings/src/com/android/settings)static void configureDatePicker(DatePicker datePicker) {        // The system clock can't represent dates outside this range.        Calendar t = Calendar.getInstance();        t.clear();        t.set(1970, Calendar.JANUARY, 1);        datePicker.setMinDate(t.getTimeInMillis());        t.clear();        t.set(2036, Calendar.DECEMBER, 31);//modify from 2037 to 2036        datePicker.setMaxDate(t.getTimeInMillis());    }

大功告成咯!
有坑再填。

原创粉丝点击