如何从 DatePickerDialog 获取 DatePicker? [英] How do you get a DatePicker from a DatePickerDialog?

查看:22
本文介绍了如何从 DatePickerDialog 获取 DatePicker?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个弹出 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));

我想从 DatePickerDialog 进入 DatePicker 视图,以便我可以设置最小和最大日期.API 参考说有一个方法 getDatePicker 并且自 API 1 以来一直存在,但它无效,我的 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();

未定义 DatePickerDialog 类型的 getDatePicker() 方法"

"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 方法仅在 Honeycomb 及更高版本中可用(正如 dmon 正确指出的那样),您仍然可以相当轻松地访问 DatePicker<DatePickerDialog 使用反射封装的/code> 对象.它看起来像这样:

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆