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

前端 未结 8 2082
后悔当初
后悔当初 2020-12-03 01:31

i want to disable the day selection option on the android sdk date picker. any easy xml configuration would be the best

相关标签:
8条回答
  • 2020-12-03 01:54

    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());
    }
    
    0 讨论(0)
  • 2020-12-03 01:55

    I think It's the best solution

      datePicker.findViewById(Resources.getSystem().getIdentifier("day", "id", "android")).setVisibility(View.GONE);
    
    0 讨论(0)
  • 2020-12-03 01:55

    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"
            />
    
    0 讨论(0)
  • 2020-12-03 01:56

    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.

    0 讨论(0)
  • 2020-12-03 02:00

    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 讨论(0)
  • 2020-12-03 02:02

    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!

    0 讨论(0)
提交回复
热议问题