How to turn off CalendarView in a DatePicker?

前端 未结 5 1335
不思量自难忘°
不思量自难忘° 2020-12-03 06:53

On my settings screen I have a date picker widget. In the designer in Eclipse, it shows as I want (3 spinners for D-M-Y) but when I test on my device, I get a rather odd vie

相关标签:
5条回答
  • 2020-12-03 06:55

    I made it work with the following XML configuration:

    android:datePickerMode="spinner"
    android:calendarViewShown="false"
    

    Only the following configuration didn't work for me:

    android:calendarViewShown="false"
    
    0 讨论(0)
  • 2020-12-03 07:05

    If you are targeting a later version of the API, you can use the following XML (no need to write Java code) in your <DatePicker>:

     android:calendarViewShown="false"
    
    0 讨论(0)
  • 2020-12-03 07:08

    The method in DatePicker

        public void setCalendarViewShown (boolean shown)
    

    exists starting with API 11. If you minSdkLevel = 7 the compiler does not recognize this as a valid method - the method does not exist on android 2.3 or 2.2. The best way is to solve this is using reflection. Something like this should work properly:

    int currentapiVersion = android.os.Build.VERSION.SDK_INT;
    if (currentapiVersion >= 11) {
      try {
        Method m = minDateSelector.getClass().getMethod("setCalendarViewShown", boolean.class);
        m.invoke(minDateSelector, false);
      }
      catch (Exception e) {} // eat exception in our case
    }
    
    0 讨论(0)
  • 2020-12-03 07:09

    In those cases I use

    import android.os.Build;
    
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public void someThing() {
        [...]
    
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            minDateSelector.setCalendarViewShown(false);
        }
    }
    

    I think the readability is better than using reflection and the style is better than catch and ignore exceptions. Of course the reflection thing is also working.

    0 讨论(0)
  • 2020-12-03 07:18

    I had the same problem as you, I couldn't make the change appear via XML.

    You are on the right track, try changing your last line to:

    minDateSelector.setCalendarViewShown(false);
    
    0 讨论(0)
提交回复
热议问题