在 Android 5.0+ Lollipop 中从 DatePicker 隐藏日、月或年 [英] Hide Day, Month, or Year from DatePicker in Android 5.0+ Lollipop

查看:13
本文介绍了在 Android 5.0+ Lollipop 中从 DatePicker 隐藏日、月或年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been searching for a solution to hide any one of the following spinners from the DatePicker. For Android 5.0 the internal variables were changed and, in my case, the Day and Month spinners were visible again after the update to my device.

Android 4.4 和 Kitkat 解决方案

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+ Lollipop 解决方案

    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);
                    }
                }
            }

我在这里找到了上述问题的解决方案:Android Lollipop 中的自定义日期选择器对话框

I found the above solution to this problem here: Custom Date Picker Dialog in Android Lollipop

我想确保它易于搜索,以便其他人可以从中受益,因此我创建了这篇新帖子.(这个解决方案花了我几个小时才找到并理解,所以我希望能帮助到这方面的人).

I wanted to make sure that it was easily searchable so others could benefit from this so I created this new post. (The solution took me several hours to locate and understand, so I hope to help someone with this).

我已经测试过了,它运行良好.其实,如果你先把Lollipop的解决方案放在它下面,在代码中,把Kitkat的解决方案放在一起,那么两个版本都兼容,互不干扰.(确保使用 try/catch ;)

I've tested this and it works perfectly. In fact, if you put the solution for the Lollipop first and then under that, in the code, put the solution for Kitkat, then it is compatible for both versions without interference. (Make sure to use try/catch ;)

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

EDIT 7.22.2015: I thought it was obvious if one was using a DatePicker that it needed to be initialized. I included the code to show that you need to initialize the DatePicker before you run the rest of the code (in both situations) otherwise your Views will throw a NullPointerException. That includes the yearSpinner throwing a null. Also, you should have at least one view for the date, don't hide all 3, i.e., don't hide the day, month, and the year at the same time.

推荐答案

仅适用于懒人,这是完全集成的代码(任何 SDK),它正在为我构建一个月份选择器(90% 等于 @Brandon 建议)):

Just for the lazy bones, this the fully integrated code (any SDK) that is working for me to build a month picker (90% equal to @Brandon suggestion):

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);
            }
        }
    }
}

这篇关于在 Android 5.0+ Lollipop 中从 DatePicker 隐藏日、月或年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆