android5.0日历范围

本文介绍了Android系统中日期选择器(DatePicker)的日期范围限制,并详细解释了如何通过修改系统代码来调整日期范围,以避免应用在特定日期下出现异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言:最近项目攻关,一大堆乱七八糟不归我管的模块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());
    }

大功告成咯!
有坑再填。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值