Android API 21+ DatePicker无法隐藏字段

时间:2016-05-10 17:10:25

标签: android datepicker show-hide android-api-levels

我试图在AlertDialog中的DatePicker中只显示月份和月份字段。在API 21的先前API中,我可以使用下面的代码来执行此操作。但是当我尝试在API21 +中执行此操作时,“getDeclaredFields()”甚至不会像“mYearPicker”或“mYearSpinner”字段那样返回。有没有人知道如何我可以只显示日期和月份字段吗?我甚至不知道字段“116”是什么。

我的代码是这样的:

 final DatePicker datePickerStart = new DatePicker(new ContextThemeWrapper(getActivity(), android.R.style.Theme_Holo_Light_Dialog_NoActionBar));
    datePickerStart .setCalendarViewShown(false);
    datePickerStart .setSpinnersShown(true);
    try {
        Field f[] = datePickerStart .getClass().getDeclaredFields();
        for (Field field : f) {
            if (field.getName().equals("mYearPicker")|| field.getName().equals("mYearSpinner")) {
                field.setAccessible(true);
                Object yearPicker = new Object();
                yearPicker = field.get(datePickerStart);
                ((View) yearPicker).setVisibility(View.GONE);
            }
        }
    }
    catch (Exception e) {
        Log.e("ERROR", e.getMessage());
    }

应该是这样的: Before API 21

看起来像这样: API 21+

有什么想法吗?

1 个答案:

答案 0 :(得分:-1)

感谢@Morse,我找到了解决方案HERE

05-10 10:28:25.057 15933-15933/com.ragemods W/System: ClassLoader referenced unknown path: /data/app/com.ragemods-1/lib/arm
05-10 10:28:25.136 15933-15933/com.ragemods V/Rage: Rage: on create called
05-10 10:28:25.157 15933-15933/com.ragemods V/Rage: Rage: app context null
05-10 10:28:25.157 15933-15933/com.ragemods V/Rage: Rage: on create view null
05-10 10:28:25.324 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:25.398 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:25.443 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:25.492 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:25.537 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:25.592 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:25.650 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:25.716 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:25.772 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:25.808 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:25.858 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:25.907 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:25.931 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:25.953 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:25.965 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:25.986 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:26.014 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:26.062 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:26.085 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:26.119 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:26.150 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:26.194 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:26.224 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:26.249 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:26.286 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:26.332 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:26.378 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:26.432 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:26.495 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:26.540 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:26.562 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:26.588 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:26.614 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:26.646 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:26.685 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:26.732 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:26.778 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:26.801 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:26.838 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:26.895 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:26.935 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:26.974 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:27.013 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:27.057 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:27.093 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:27.123 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:27.147 15933-15933/com.ragemods D/MediaPlayer: setSubtitleAnchor in MediaPlayer
05-10 10:28:27.535 15933-15933/com.ragemods V/!!!!!!load!!!!!: loaddone
05-10 10:28:27.542 15933-15933/com.ragemods V/Rage: Rage: on resume called
05-10 10:28:27.549 15933-16163/com.ragemods D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
05-10 10:28:27.610 15933-16163/com.ragemods I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: QUALCOMM Build: 10/21/15, 369a2ea, I96aee987eb
05-10 10:28:27.613 15933-16163/com.ragemods I/OpenGLRenderer: Initialized EGL, version 1.4
05-10 10:28:28.605 15933-16162/com.ragemods V/rage spawn people: Rage spawn people Took 1.0590515 seconds
05-10 10:28:30.643 15933-16162/com.ragemods V/rage spawn cars: Rage spawn cars Took 1.34906 seconds
05-10 10:28:33.595 15933-15940/com.ragemods W/art: Suspending all threads took: 7.141ms
05-10 10:28:43.697 15933-15940/com.ragemods W/art: Suspending all threads took: 35.125ms
05-10 10:28:44.672 15933-15940/com.ragemods W/art: Suspending all threads took: 7.293ms
05-10 10:29:02.807 15933-15940/com.ragemods W/art: Suspending all threads took: 45.379ms
05-10 10:29:13.527 15933-15933/com.ragemods V/rage spawn people: Rage spawn people Took 0.8362427 seconds
05-10 10:29:14.905 15933-15933/com.ragemods V/rage spawn cars: Rage spawn cars Took 0.8807373 seconds
05-10 10:29:14.912 15933-15933/com.ragemods I/Choreographer: Skipped 133 frames!  The application may be doing too much work on its main thread.
05-10 10:29:36.407 15933-15940/com.ragemods W/art: Suspending all threads took: 15.502ms
05-10 10:29:51.115 15933-15933/com.ragemods E/MediaPlayer: internal/external state mismatch corrected
05-10 10:30:12.128 15933-15933/com.ragemods V/rage spawn people: Rage spawn people Took 0.8179322 seconds
05-10 10:30:13.507 15933-15933/com.ragemods V/rage spawn cars: Rage spawn cars Took 0.86376953 seconds
05-10 10:30:31.199 15933-15933/com.ragemods V/rage spawn people: Rage spawn people Took 0.8010864 seconds
05-10 10:30:32.617 15933-15933/com.ragemods V/rage spawn cars: Rage spawn cars Took 0.9103699 seconds
05-10 10:30:32.621 15933-15933/com.ragemods I/Choreographer: Skipped 134 frames!  The application may be doing too much work on its main thread.
05-10 10:30:46.152 15933-15940/com.ragemods W/art: Suspending all threads took: 22.155ms
05-10 10:30:53.682 15933-15944/com.ragemods I/art: Background sticky concurrent mark sweep GC freed 805761(15MB) AllocSpace objects, 0(0B) LOS objects, 33% free, 30MB/45MB, paused 671us total 153.503ms
05-10 10:30:57.850 15933-15989/com.ragemods W/System: ClassLoader referenced unknown path: /data/data/com.ragemods/lib
05-10 10:30:57.937 15933-15933/com.ragemods V/!!!!!!savedone!!!!!: savedone
05-10 10:30:57.939 15933-15933/com.ragemods V/Rage: Rage: on create called
05-10 10:30:57.942 15933-15933/com.ragemods V/Rage: Rage: app context null
05-10 10:30:57.942 15933-15933/com.ragemods V/Rage: Rage: on resume called
05-10 10:30:57.976 15933-16163/com.ragemods V/RenderScript: 0x9f0f5000 Launching thread(s), CPUs 4
05-10 10:31:01.753 15933-15940/com.ragemods W/art: Suspending all threads took: 5.462ms
05-10 10:31:06.486 15933-15944/com.ragemods I/art: Background sticky concurrent mark sweep GC freed 912685(16MB) AllocSpace objects, 0(0B) LOS objects, 34% free, 29MB/45MB, paused 579us total 104.888ms
05-10 10:31:12.996 15933-15944/com.ragemods I/art: Background sticky concurrent mark sweep GC freed 860407(16MB) AllocSpace objects, 0(0B) LOS objects, 34% free, 30MB/45MB, paused 396us total 101.684ms
05-10 10:31:15.471 15933-15933/com.ragemods A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 15933 (com.ragemods)

所以我只需要在需要新的DatePicker对象时调用此方法