Android Lollipop 中的自定义日期选择器对话框 [英] Custom Date Picker Dialog in Android Lollipop

查看:22
本文介绍了Android Lollipop 中的自定义日期选择器对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个日期选择器只显示月份和年份.我已经自定义了日期选择器来这样做,即从选择器中删除天"字段,但在 Android Lollipop 中,我正在获取带有日、月和年的选择器.以下是我的一段代码.请帮助我了解问题所在.提前致谢.

 试试 {字段 f[] = mDatePicker.getClass().getDeclaredFields();对于(字段字段:f){if (field.getName().equals("mDaySpinner") || field.getName().equals("mDayPicker")) {field.setAccessible(true);Object dayPicker = new Object();dayPicker = field.get(mDatePicker);((View) dayPicker).setVisibility(View.GONE);}}} catch (SecurityException e) {} catch (IllegalArgumentException e) {} catch (IllegalAccessException e) {}

解决方案

使用反射来查找和隐藏 UI 元素并不是一个很好的做法.在您的情况下,它停止在棒棒糖中工作,因为 mDaySpinner 现在包含在 DatePicker 类中的内部私有静态 DatePickerSpinngerDelegate 类中.>

我建议通过视图层次结构来查找和隐藏日微调器元素.我编写了以下适用于棒棒糖的代码:

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

I want a date picker to show only Month and Year. I've customized the Date Picker to do so i.e., to remove 'day' field from the picker,but in Android Lollipop Am getting picker with Day, Month and Year. Following is my piece of code. Please help me to know the problem. Thanks in advance.

    try {
        Field f[] = mDatePicker.getClass().getDeclaredFields();
        for (Field field : f) {

            if (field.getName().equals("mDaySpinner") || field.getName().equals("mDayPicker")) {
                field.setAccessible(true);
                Object dayPicker = new Object();
                dayPicker = field.get(mDatePicker);
                ((View) dayPicker).setVisibility(View.GONE);
            }

        }
    } catch (SecurityException e) {
    } catch (IllegalArgumentException e) {
    } catch (IllegalAccessException e) {
    }

解决方案

Using reflection to find and hide UI elements is not really a great practice. In your case, it stopped working in lollipop because the mDaySpinner is now contained in an internal private static DatePickerSpinngerDelegate class within the DatePicker class.

I would recommend going through the view hierarchy to find and hide the day spinner element instead. I wrote the following code that works in lollipop:

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

这篇关于Android Lollipop 中的自定义日期选择器对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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