开发者

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

开发者 https://www.devze.com 2023-01-23 19:33 出处:网络
i want to disable the day se开发者_运维知识库lection option on the android sdk date picker. any easy xml configuration would be the bestI just have released a new date picker framework which allows yo

i want to disable the day se开发者_运维知识库lection option on the android sdk date picker. any easy xml configuration would be the best


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

UPDATE 06/2014: This library was developed 2010 and has been unmaintained since 2011. So it is most likely out of date by now.


It possible to hack the DatePicker instance using reflection. This way, you are able to access the NumberPicker instance which represent the day in the 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

"mDaySpinner"

instead

"mDayPicker"


I think It's the best solution

  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. 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!


This works fine in 2.2 version. 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);
            }
        }
    }
}


You can use the https://github.com/SimonVT/android-datepicker widget for backported compatibility and make the day picker 'gone'

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

精彩评论

暂无评论...
验证码 换一张
取 消