Hide Year field in Android DatePicker?

前端 未结 5 585
梦毁少年i
梦毁少年i 2020-12-15 22:41

I\'m using a basic DatePicker and I\'m trying to figure out how to hide the Year field on the DatePickerDialog so that only the Month and Day are visible. I don\'t mind that

5条回答
  •  再見小時候
    2020-12-15 23:40

    A super easy way that I found to implement a DatePicker is to call it in xml:

        
    

    and then hide whichever field you want (in this case the year) in java:

        picker = (DatePicker) findViewById(R.id.thePicker);
        try {
            Field f[] = picker.getClass().getDeclaredFields();
            for (Field field : f) {
                if (field.getName().equals("mYearPicker")) {
                    field.setAccessible(true);
                    Object yearPicker = new Object();
                    yearPicker = field.get(picker);
                    ((View) yearPicker).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());
        }
    

提交回复
热议问题