Hide Year field in Android DatePicker?

前端 未结 5 580
梦毁少年i
梦毁少年i 2020-12-15 22:41

I\'m using a basic DatePicker and I\'m trying to figure out how to hide the Year field on the DatePickerDialog so that only the Month and Day are visible. I don\'t mind that

相关标签:
5条回答
  • 2020-12-15 23:15

    Easy way to disable Year field in date picker

    DatePicker dp = findDatePicker((ViewGroup) dialog.getWindow().getDecorView());
    
    if(dp != null) 
    {
        ((ViewGroup) ((ViewGroup) dp.getChildAt(0)).getChildAt(0)).getChildAt(2).setEnabled(false);
        ((ViewGroup) ((ViewGroup) dp.getChildAt(0)).getChildAt(0)).getChildAt(2).setAlpha(0.4f);
    }
    

    if you want to remove year field just setvisibility of result view to View.GONE like below

    dp.getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE)
    
    
    
    private DatePicker findDatePicker(ViewGroup group) {
        if (group != null) {
            for (int i = 0, j = group.getChildCount(); i < j; i++) {
                View child = group.getChildAt(i);
                if (child instanceof DatePicker) {
                    return (DatePicker) child;
                } else if (child instanceof ViewGroup) {
                    DatePicker result = findDatePicker((ViewGroup) child);
                    if (result != null)
                        return result;
                }
            }
        }
        return null;
    
    }
    

    I have refered below link for reference Remove year from DatePickerDialog

    0 讨论(0)
  • 2020-12-15 23:27

    Be really careful with this solution. you are using reflexion to access a field and change the visibility. This will not work with Android 5.0 ! Google have changed the implementation of the TimePicker and DatePicker and the classes are no longer exposing any fields appart from the delegate.

    I don't know the solution yet for Android 5.0+.I will let you know as soon I found something interesting.

    As an idea, you could use the Build.VERSION.SDK_INT to check the Android version and then try to access the DatePickerSpinnerDelegate using reflexion and set the field you want.

    Again, this is not the ideal solution as Google are free to change this again and again...

    0 讨论(0)
  • 2020-12-15 23:29

    You can set range of the year by using this function

    datePicker.setYearRange(1950,2017);
    
    0 讨论(0)
  • 2020-12-15 23:30

    Recently managed to hide the year part of the dialog by setting the datepicker width too small.

    <DatePicker
            android:id="@+id/month_day_datepicker"
            android:layout_width="140dp"
            android:datePickerMode="spinner"
            style="?android:attr/borderlessButtonStyle"/>
    

    Let me know if this works, obviously, there is an accessible year in the binding but you can just not pick that up when calculating the date.

    0 讨论(0)
  • 2020-12-15 23:40

    A super easy way that I found to implement a DatePicker is to call it in xml:

        <DatePicker
        android:id="@+id/thePicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    

    and then hide whichever field you want (in this case the year) in java:

        picker = (DatePicker) findViewById(R.id.thePicker);
        try {
            Field f[] = picker.getClass().getDeclaredFields();
            for (Field field : f) {
                if (field.getName().equals("mYearPicker")) {
                    field.setAccessible(true);
                    Object yearPicker = new Object();
                    yearPicker = field.get(picker);
                    ((View) yearPicker).setVisibility(View.GONE);
                }
            }
        } 
        catch (SecurityException e) {
            Log.d("ERROR", e.getMessage());
        } 
        catch (IllegalArgumentException e) {
            Log.d("ERROR", e.getMessage());
        } 
        catch (IllegalAccessException e) {
            Log.d("ERROR", e.getMessage());
        }
    
    0 讨论(0)
提交回复
热议问题