问题描述
我有一个弹出一个android.app.DatePickerDialog一个活动。
I have a activity that is popping up a android.app.DatePickerDialog.
DatePickerDialog dialog = new DatePickerDialog(this, startDateSetListener, start_cal.get(Calendar.YEAR), start_cal.get(Calendar.MONTH), start_cal.get(Calendar.DATE));
我想在的DatePicker查看从DatePickerDialog,这样我可以设置最小和最大的日期。该API的参考表示,有一种方法getDatePicker,自1 API已经存在,但它是无效的,我的IDE不喜欢它。
I want to get at the DatePicker View from the DatePickerDialog so that I can set the min and max dates. The API reference says that there is a method getDatePicker and that has been there since API 1 but it is invalid, my ide doesn't like it.
DatePicker picker = dialog.getDatePicker();
的方法getDatePicker()是未定义的类型DatePickerDialog
"The method getDatePicker() is undefined for the type DatePickerDialog"
任何一个知道什么API是在方法中创建或者我怎么能到达的DatePicker编辑最小/最大日期?
Any one know what API was the method created in or how I can get to the DatePicker to edit min/max dates?
我使用的API 7,平台2.1。
I am using API 7, Platform 2.1.
推荐答案
尽管getter方法仅是在蜂窝及以上(为DMON正确地指出的)可用,你仍然可以很方便地访问该的DatePicker
对象的 DatePickerDialog
使用反射封装。它会是这个样子:
Despite that getter method only being available in Honeycomb and above (as dmon correctly pointed out), you can still fairly easily get access to the DatePicker
object encapsulated by the DatePickerDialog
using reflection. It'll look something like this:
DatePickerDialog dialog = ... // create dialog
Field mDatePickerField = dialog.getClass().getDeclaredField("mDatePicker");
mDatePickerField.setAccessible(true);
DatePicker mDatePickerInstance = (DatePicker) mDatePickerField.get(dialog);
从这个角度上,你有一个参考的内部的DatePicker
对象,你应该能够将其变为自己的喜好。请不要提防任何明显的错别字,因为我进入了这个直接进入浏览器。
From this point on, you have a reference to the internal DatePicker
object and you should be able to change it to your own liking. Please do beware of any obvious typos, as I entered this directly into the browser.
这篇关于你如何从一个DatePickerDialog一个datepicker?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!