在 Android 5.0+ Lollipop 中从 DatePicker 隐藏日、月或年

Hide Day, Month, or Year from DatePicker in Android 5.0+ Lollipop

我一直在寻找一种解决方案来从 DatePicker 中隐藏以下任一微调器。对于 Android 5.0,内部变量发生了变化,在我的例子中,在我的设备更新后,日期和月份微调器再次可见。

Android 4.4 和奇巧解决方案

DatePicker dpDate = (DatePicker) findViewById(R.id.dpDate);

    // Initialize Date Picker
    int year    = dpDate.getYear();
    int month   = dpDate.getMonth();
    int day     = dpDate.getDayOfMonth();
    dpDate.init(year, month, day, this);

Field f[] = dpDate.getClass().getDeclaredFields();
            for (Field field : f) 
            {
                // Hides the DAY spinner
                if(field.getName().equals("mDayPicker") || field.getName().equals("mDaySpinner")) 
                {
                    field.setAccessible(true);
                    Object dayPicker = new Object();
                    dayPicker = field.get(dpDate);
                    ((View) dayPicker).setVisibility(View.GONE);
                }

                // Hides the MONTH spinner
                if(field.getName().equals("mMonthPicker") || field.getName().equals("mMonthSpinner")) 
                {
                    field.setAccessible(true);
                    Object monthPicker = new Object();
                    monthPicker = field.get(dpDate);
                    ((View) monthPicker).setVisibility(View.GONE);
                }

                // Hides the YEAR spinner
                if(field.getName().equals("mYearPicker") || field.getName().equals("mYearSpinner")) 
                {
                    field.setAccessible(true);
                    Object yearPicker = new Object();
                    yearPicker = field.get(dpDate);
                    ((View) myearPicker).setVisibility(View.GONE);
                }
            }

Android 5.0+ 棒棒糖解决方案

    DatePicker dpDate = (DatePicker) findViewById(R.id.dpDate);

    // Initialize Date Picker
    int year    = dpDate.getYear();
    int month   = dpDate.getMonth();
    int day     = dpDate.getDayOfMonth();
    dpDate.init(year, month, day, this);

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) 
            {
                int daySpinnerId = Resources.getSystem().getIdentifier("day", "id", "android");
                if (daySpinnerId != 0) 
                {
                    View daySpinner = dpDate.findViewById(daySpinnerId);
                    if (daySpinner != null) 
                    {
                        daySpinner.setVisibility(View.GONE);
                    }
                }
            }

            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) 
            {
                int monthSpinnerId = Resources.getSystem().getIdentifier("month", "id", "android");
                if (monthSpinnerId != 0) 
                {
                    View monthSpinner = dpDate.findViewById(monthSpinnerId);
                    if (monthSpinner != null) 
                    {
                        monthSpinner.setVisibility(View.GONE);
                    }
                }
            }

            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) 
            {
                int yearSpinnerId = Resources.getSystem().getIdentifier("year", "id", "android");
                if (yearSpinnerId != 0) 
                {
                    View yearSpinner = dpDate.findViewById(yearSpinnerId);
                    if (yearSpinner != null) 
                    {
                        yearSpinner.setVisibility(View.GONE);
                    }
                }
            }

我在这里找到了上述问题的解决方案: Custom Date Picker Dialog in Android Lollipop

我想确保它易于搜索,以便其他人可以从中受益,所以我创建了这个新的 post。 (这个解决方案花了我几个小时来定位和理解,所以我希望能帮助到这个人)。

我已经对此进行了测试,它运行良好。事实上,如果你把 Lollipop 的解决方案放在前面,然后在它下面,在代码中,放上 Kitkat 的解决方案,那么它是兼容两个版本的,没有干扰。 (确保使用 try/catch ;)

编辑 2015 年 7 月 22 日: 我认为很明显,如果一个人正在使用 DatePicker,它需要被初始化。我包含的代码表明您需要在 运行 其余代码(在两种情况下)之前初始化 DatePicker,否则您的视图将抛出 NullPointerException。这包括抛出 null 的 yearSpinner。此外,您应该至少有一个日期视图,不要隐藏所有 3 个,即不要同时隐藏日、月和年。

只是为了懒惰的骨头,这是完全集成的代码(任何 SDK),正在为我构建一个月份选择器(90% 等于@Brandon 建议):

public void initMonthPicker(){
    dp_mes = (DatePicker) findViewById(R.id.dp_mes);

    int year    = dp_mes.getYear();
    int month   = dp_mes.getMonth();
    int day     = dp_mes.getDayOfMonth();

    dp_mes.init(year, month, day, new DatePicker.OnDateChangedListener() {
        @Override
        public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            month_i = monthOfYear + 1;
            Log.e("selected month:", Integer.toString(month_i));
         //Add whatever you need to handle Date changes
        }
    });

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
        int daySpinnerId = Resources.getSystem().getIdentifier("day", "id", "android");
        if (daySpinnerId != 0)
        {
            View daySpinner = dp_mes.findViewById(daySpinnerId);
            if (daySpinner != null)
            {
                daySpinner.setVisibility(View.GONE);
            }
        }

        int monthSpinnerId = Resources.getSystem().getIdentifier("month", "id", "android");
        if (monthSpinnerId != 0)
        {
            View monthSpinner = dp_mes.findViewById(monthSpinnerId);
            if (monthSpinner != null)
            {
                monthSpinner.setVisibility(View.VISIBLE);
            }
        }

        int yearSpinnerId = Resources.getSystem().getIdentifier("year", "id", "android");
        if (yearSpinnerId != 0)
        {
            View yearSpinner = dp_mes.findViewById(yearSpinnerId);
            if (yearSpinner != null)
            {
                yearSpinner.setVisibility(View.GONE);
            }
        }
    } else { //Older SDK versions
        Field f[] = dp_mes.getClass().getDeclaredFields();
        for (Field field : f)
        {
            if(field.getName().equals("mDayPicker") || field.getName().equals("mDaySpinner"))
            {
                field.setAccessible(true);
                Object dayPicker = null;
                try {
                    dayPicker = field.get(dp_mes);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                ((View) dayPicker).setVisibility(View.GONE);
            }

            if(field.getName().equals("mMonthPicker") || field.getName().equals("mMonthSpinner"))
            {
                field.setAccessible(true);
                Object monthPicker = null;
                try {
                    monthPicker = field.get(dp_mes);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                ((View) monthPicker).setVisibility(View.VISIBLE);
            }

            if(field.getName().equals("mYearPicker") || field.getName().equals("mYearSpinner"))
            {
                field.setAccessible(true);
                Object yearPicker = null;
                try {
                    yearPicker = field.get(dp_mes);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                ((View) yearPicker).setVisibility(View.GONE);
            }
        }
    }
}

非常简单,但不直观。

先把主题改成DialogPicker

new DatePickerDialog(getActivity(),android.R.style.Theme_Holo_Dialog, this, year,month, day);

现在,是的,您可以隐藏任何微调器。

dialog.getDatePicker().findViewById(getResources().getIdentifier("day","id","android")).setVisibility(View.GONE);

我在 5.0.2 中工作正常 API 21

它对 5 岁和 6 岁以上儿童有不同的 ID。我已经处理了以下代码。

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            int yearSpinnerId = Resources.getSystem().getIdentifier("date_picker_year", "id", "android");
            if (yearSpinnerId != 0)
            {
                View yearSpinner = datePicker.findViewById(yearSpinnerId);
                if (yearSpinner != null)
                    yearSpinner.setVisibility(View.GONE);
            }
        }

        if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            int yearSpinnerId = Resources.getSystem().getIdentifier("date_picker_header_year", "id", "android");
            if (yearSpinnerId != 0)
            {
                View yearSpinner = datePicker.findViewById(yearSpinnerId);
                if (yearSpinner != null)
                    yearSpinner.setVisibility(View.GONE);
            }
        }