【问题标题】:Hide Day, Month, or Year from DatePicker in Android 5.0+ Lollipop在 Android 5.0+ Lollipop 中隐藏 DatePicker 中的日、月或年
【发布时间】:2015-08-27 16:28:54
【问题描述】:

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

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

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

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

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

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

【问题讨论】:

  • 在此代码中“查看”始终为空..
  • 你必须更具体地说明你在说什么......
  • 我已经使用了上面的代码,我得到“yearSpinner”为空。应该是什么问题?
  • 我已经对上面的代码进行了编辑,以解释为什么你们都得到了 NullPointerException。确保在尝试隐藏视图之前初始化 DatePicker,否则它将不知道该怎么做。看看上面的编辑。我还要说,您需要在 onCreate(对于 Activity)或 onCreateView(对于 Fragment)或 onCreateDialog(对于 DialogFragment)中声明 DatePicker。在声明之后在单独的方法中将其初始化。

标签: android datepicker


【解决方案1】:

非常简单,但不直观。

先把主题从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 中工作正常

【讨论】:

  • 尝试了一百万种方法,这就是解决它的方法!
【解决方案2】:

对于懒惰的人来说,这是完全集成的代码(任何 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);
            }
        }
    }
}

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-17
      相关资源
      最近更新 更多