简体   繁体   English

如何显示仅具有月份和年份字段的Android日期选择器?

[英]How to display date picker for android with only month and year fields?

i want to disable the day selection option on the android sdk date picker. 我想在android sdk日期选择器上禁用日期选择选项。 any easy xml configuration would be the best 任何简单的xml配置都是最好的

I just have released a new date picker framework which allows you to create custom date picker. 我刚刚发布了一个新的日期选择器框架,该框架允许您创建自定义日期选择器。 I also provided some example date pickers like the one you are looking for. 我还提供了一些示例日期选择器,例如您要查找的日期选择器。 Hope, that it works for you. 希望它对您有用。

the code can be found here: https://github.com/bendemboski/DateSlider 该代码可以在这里找到: https : //github.com/bendemboski/DateSlider

UPDATE 06/2014: This library was developed 2010 and has been unmaintained since 2011. So it is most likely out of date by now. 更新06/2014:此库是2010年开发的,自2011年以来一直未维护。因此,现在很可能已经过时。

It possible to hack the DatePicker instance using reflection. 可以使用反射来破解DatePicker实例。 This way, you are able to access the NumberPicker instance which represent the day in the DatePicker: 这样,您就可以访问NumberPicker实例,该实例代表DatePicker中的日期:

datePicker = (DatePicker) findViewById(R.id.expiration_date);
try {
    Field f[] = datePicker.getClass().getDeclaredFields();
    for (Field field : f) {
        if (field.getName().equals("mDayPicker")) {
            field.setAccessible(true);
            Object dayPicker = new Object();
            dayPicker = field.get(datePicker);
            ((View) dayPicker).setVisibility(View.GONE);
        }
    }
} catch (SecurityException e) {
    Log.d("ERROR", e.getMessage());
} 
catch (IllegalArgumentException e) {
    Log.d("ERROR", e.getMessage());
} catch (IllegalAccessException e) {
    Log.d("ERROR", e.getMessage());
}

Speaking of reflection this works in Android 4.4.2 SDK 说到反思,这在Android 4.4.2 SDK中有效

"mDaySpinner"

instead 代替

"mDayPicker"

我认为这是最好的解决方案

  datePicker.findViewById(Resources.getSystem().getIdentifier("day", "id", "android")).setVisibility(View.GONE);

EDIT: as per the comment below, I wouldn't follow this answer anymore. 编辑:根据下面的评论,我不会再遵循这个答案了。

I don't think you can do that with the default date picker. 我认为您无法使用默认的日期选择器来做到这一点。 You'll have to create it out of basic Android UI elements. 您必须使用基本的Android UI元素来创建它。 But instead of going to that trouble, why not use a better library element like this great-looking iPhone-esque wheel: http://code.google.com/p/android-wheel/ I haven't used it yet, but I plan to! 但是,除了麻烦之外,为什么不使用更好的库元素,例如外观精美的iPhone式转盘: http : //code.google.com/p/android-wheel/我还没有使用过,但是我打算!

You can use the https://github.com/SimonVT/android-datepicker widget for backported compatibility and make the day picker 'gone' 您可以使用https://github.com/SimonVT/android-datepicker窗口小部件以实现向后移植的兼容性,并使日期选择器“消失”

<net.simonvt.numberpicker.NumberPicker
        android:id="@+id/day"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dip"
        android:layout_marginRight="16dip"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:visibility="gone"
        />

For Hiding Day : 隐藏日:

 datePicker.findViewById(Resources.getSystem().getIdentifier("day", "id", "android")).setVisibility(View.GONE);

&

For hiding Month : 隐藏月份:

datePicker.findViewById(Resources.getSystem().getIdentifier("month", "id", "android")).setVisibility(View.GONE);

This works fine in 2.2 version. 在2.2版本中可以正常工作。 But title of the dialog is not changed. 但是对话框的标题未更改。

for (Field datePickerDialogField : datePickerDialogFields) {           

    if (datePickerDialogField.getName().equals("mDatePicker")) {
        datePickerDialogField.setAccessible(true);
        DatePicker datePicker = (DatePicker) datePickerDialogField.get(datePickerDialog);

        Field datePickerFields[] = datePickerDialogField.getType().getDeclaredFields();

        for (Field datePickerField : datePickerFields) {

            if ("mDayPicker".equals(datePickerField.getName())) {
                datePickerField.setAccessible(true);
                Object dayPicker = new Object();
                dayPicker = datePickerField.get(datePicker);
                ((View) dayPicker).setVisibility(View.GONE);
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM