DatePickerDialog incorrectly enforcing a minimum date of Jan 1, 1970

▼魔方 西西 提交于 2019-12-18 21:16:38

问题


We have a member reporting that he is unable to set a date before Jan 1, 1970 on our DatePickerDialog. This issue does not repro for us.

I am already aware that the DatePickerDialog does not expose the setMinDate/setMaxDate functions of the underlying DatePicker, so it would seem that some kind of handset maker-specific modification is affecting the minDate/maxDate.

This user reports he is running a Droid x2 on Verizon running 2.2 Froyo. While we believe he is correct in his description of his device model, many users are confused about the OS version, so he may be running 2.3.

I attempted to solve this problem by adding this theme to my Activity:

<style name="profile_editor_theme">     
    <item name="android:endYear">2025</item>
    <item name="android:startYear">1910</item>
</style>

While this theme on my activity had the intended effect of constraining the DatePickerDialog on my test devices (a Galaxy tab and an original Motorola Droid), it apparently had no effect for the user.

This issue repros for our user 100% of the time, but works correctly for us on our own devices.

Can anyone explain what might be causing this and how we could fix it?

I have filed this bug against Google on this matter.

Thanks!


回答1:


Starting date of Android devices starts from Jan 1, 1970. Maybe this can be your case. Android calculates time as a number of milliseconds passed since Jan 1, 1970.

I've found a kind of hack for your case. Here I create dynamically datePicker:

     DatePicker dp = new DatePicker(this);
     dp.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
     v.addView(dp);

In the manifest file I declare a custom theme for my application - I want the same theme for the application. By the way you can do the same for activity.

 <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" 
    android:theme="@style/CustomTheme">
    <activity
        android:name=".HelloDatePickerActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

In a styles.xml I do this:

<style name="CustomTheme" parent="android:Theme.Light">
    <item name="android:startYear">1890</item>
</style>

The default start date in my case is 1900. Thus, for me this approach works.

Hope this will help you!




回答2:


I realize this question is a little old, and that this answer will not apply to older devices, but I've recently been made aware of this issue on a newer device (the Huawei Mediapad 7 Youth), so I figured I'd update this with newer information:

Starting with API 11, the methods necessary to do this are exposed. Remember, while Calendar may use Unix Epoch as zero, Java (at least until ~JRE 8?) uses signed numbers, so negatives are completely acceptable. Thankfully you don't need to be fully aware of this, as this code runs fine:

// This is API 11!
try {
    // Hack to (try to) force minDate to 1888 instead of 1970.
    DatePicker dp = dialog.getDatePicker();
    Calendar minCal = Calendar.getInstance();
    minCal.set(1888, Calendar.JANUARY, 1, 0, 0, 0);
    dp.setMinDate(minCal.getTimeInMillis());

    if (maxDate > 0)
        dp.setMaxDate(maxDate);
} catch (NoSuchMethodError e) {
    Log.w(TAG, "Can't set min/max dates: device/Android version is too old.");
}

I used 1888 so that any reports stating this is the minimum will make it clear that the code was executed and not ignored/overridden. I should note I have not yet heard back whether or not this works on the reported device.




回答3:


Hi I found the solution for it. you can set the -ve value in the setMinDate() method and it works for me.

dp.setMinDate(-1576800000000L);



回答4:


I see you already tried something similar, but have you tried setting the following:

android:startYear="1900"

directly on the DatePicker XML?

Like so:

<DatePicker
          android:startYear="1900"
          android:endYear="2100"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
></DatePicker>

Reference: http://kevsaidwhat.blogspot.com/2011/10/fun-with-android-datepicker-and-1970.html



来源:https://stackoverflow.com/questions/8620781/datepickerdialog-incorrectly-enforcing-a-minimum-date-of-jan-1-1970

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!