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

前端 未结 8 2130
后悔当初
后悔当初 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());
    }
    

提交回复
热议问题