Android最简单方式实现DatePickerDialog只显示年、月、日

@百晓生2017-11-2 2915

  项目刚好有这个需求,上网查了下。有个例子还比较简单。自定义实现DatePickerDialog,然后通过反射机制隐藏里面的控件。不过这还不是最方便的,毕竟还要不同的控件代码,XML代码。直接拿到核心代码,简单丰富一下。直接调用就可以隐藏日,月选择了。

  调用代码:

            dateDialog = new DatePickerDialog(getActivity(), listener,
                    year, monthOfYear, dayOfMonth) {
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                }
            };
            hide(dateDialog.getDatePicker(), false);
            dateDialog.show();

这个hide就是精简后的一个方法

    private void hide(DatePicker mDatePicker, boolean bShowMonth) {
        try {
            /* 处理android5.0以上的特殊情况 */
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                int daySpinnerId = Resources.getSystem().getIdentifier("day", "id", "android");
                int monthSpinnerId = Resources.getSystem().getIdentifier("month", "id", "android");
                if (daySpinnerId != 0) {
                    View daySpinner = mDatePicker.findViewById(daySpinnerId);
                    if (daySpinner != null) {
                        daySpinner.setVisibility(View.GONE);
                    }
                }
                if (!bShowMonth) {
                    if (monthSpinnerId != 0) {
                        View monthSpinner = mDatePicker.findViewById(monthSpinnerId);
                        if (monthSpinner != null) {
                            monthSpinner.setVisibility(View.GONE);
                        }
                    }
                }
            } else {
                Field[] datePickerfFields = mDatePicker.getClass().getDeclaredFields();
                for (Field datePickerField : datePickerfFields) {
                    if ("mDaySpinner".equals(datePickerField.getName()) || ("mDayPicker").equals(datePickerField.getName())) {
                        datePickerField.setAccessible(true);
                        Object dayPicker = new Object();
                        try {
                            dayPicker = datePickerField.get(mDatePicker);
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        } catch (IllegalArgumentException e) {
                            e.printStackTrace();
                        }
                        ((View) dayPicker).setVisibility(View.GONE);
                    }
                    if (!bShowMonth) {
                        if ("mMonthSpinner".equals(datePickerField.getName()) || ("mMonthPicker").equals(datePickerField.getName())) {
                            datePickerField.setAccessible(true);
                            Object monthPicker = new Object();
                            try {
                                monthPicker = datePickerField.get(mDatePicker);
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            } catch (IllegalArgumentException e) {
                                e.printStackTrace();
                            }
                            ((View) monthPicker).setVisibility(View.GONE);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

效果图:

本文链接:https://www.it72.com/12278.htm

推荐阅读
最新回复 (0)
    返回