Can't access DatePicker.ValidationCallback in my code

醉酒当歌 提交于 2019-12-25 06:22:56

问题


I have a simple DatePickerFragment that extends DialogFragment. I'm wanting to set a validation callback so I can prevent "OK" being selected if an invalid date is picked. I first tried setMaxDate and that grays out the invalid choices but still allows them to be selected and OK to be pressed. So I was hoping setting the validation callback would be the solution.

abstract public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    public DatePickerDialog datePickerDialog;

  //  public DatePickerFragment() {
  //
  //  }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        datePickerDialog = new DatePickerDialog(getActivity(), this, year, month, day);
        DatePicker datePicker = datePickerDialog.getDatePicker();
        datePicker.setMaxDate(c.getTimeInMillis());

        // this won't resolve!
        datePicker.setValidationCallback( xxx );

        return datePickerDialog;
    }

}

I cannot figure out why I can't access or reference android.widget.DatePicker.ValidationCallback or any of the related bits in my code. If I hit CTRL+B to look at DatePickerDialog I can see it imports it and references these fields. Everything appears to be public. But I can't even get the import to resolve. What could be wrong? This is a brand new install of the latest Android Studio and my gradle file looks like this:

dependencies {
    // Include local libraries
    compile fileTree(dir: 'libs', include: ['*.jar'])
    // Android support libraries
    compile 'com.android.support:support-v4:22.1.1'
    compile 'com.android.support:support-v13:22.1.1'
    compile 'com.android.support:appcompat-v7:22.1.1'
    compile 'com.android.support:recyclerview-v7:22.1.1'
    compile 'com.android.support:cardview-v7:22.1.1'
}

The path of the DatePickerDialog.java that Android Studio takes me to is E:\src\Android\sdk\sources\android-22\android\app\DatePickerDialog.java. That combined with the gradle settings leads me to believe everything I'm looking at should all be API 22... I'm at total loss here. Any ideas?

Thanks!


回答1:


That was introduced around API version 21. If you MinSDK is lower you won't have visibility to that feature.

However I just tested this by creating a sample project from scratch setting the min/max SDK to 22 and I still don't have visibility to that method. Potentially this is a bug in Android Studio.



来源:https://stackoverflow.com/questions/30289297/cant-access-datepicker-validationcallback-in-my-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!