Android - OnDateChangedListener - 如何设置?
Android 中有一个名为 DatePicker.OnDateChangedListener 的事件监听器。 我正在尝试设置 DatePicker 视图的日期更改侦听器,如下所示:
DatePicker dp = new DatePicker(getContext());
dp.setOnDateChangedListener(this);
//where this is my activity extends DatePicker.OnDateChangedListener
但是你猜怎么着? 日期选择器没有名为 setOnDateChangedListener 的方法。
我的问题是:
- 那么如何在 Android 中设置日期更改侦听器?
- 如果无法设置日期更改监听器,那么此事件的目的是什么?
任何文档/教程都会非常有帮助。
There is an event listener in Android called DatePicker.OnDateChangedListener.
I am trying to set a DatePicker view's on date changed listener as follows:
DatePicker dp = new DatePicker(getContext());
dp.setOnDateChangedListener(this);
//where this is my activity extends DatePicker.OnDateChangedListener
But guess what?
Date picker does not have a method called setOnDateChangedListener.
My question is:
- How then do you set a date changed listener in Android?
- If it is not possible to set a date changed listener, what is the purpose for this event?
Any documentation/tutorials will be very helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
创建
DatePicker
后,您需要首先使用要显示的日期对其进行初始化。此时您可以添加您的侦听器。请参阅
DatePicker.init(int, int, int, OnDateChangedListener)
。更新
26 API允许设置监听器:
DatePicker.setOnDateChangedListener()
Once you've created your
DatePicker
, you need to initialize it with the date you want to display at first. That's the point at which you can add your listener.See
DatePicker.init(int, int, int, OnDateChangedListener)
.Update
26 API allows to set listener:
DatePicker.setOnDateChangedListener()
最好的办法是
Best way is
这个视图实际上是四个视图的组合,它们是:
三个 Spinners
一个 CalendarView
对于 OnDateChangeListener,您传入 init 方法的对象将简单地传递给包含的 CalendarView,我相信您知道有是老好用的CalendarView中的setOnDateChangeListener方法…………
在DatePicker类中,有一个叫getCalendarView的方法,如果你想上手就可以调用这个方法包含的 CalendarView。
一旦你掌握了包含的 CalendarView,那么,不用说,你可以调用它的 setOnDateChangeListener
This view is in fact a combination of four views, and they are :
Three Spinners
One CalendarView
As of the OnDateChangeListener, the object you passed in to the init method will be simply passed to the contained CalendarView, and I believe that you know that there is a setOnDateChangeListener method in the good old CalendarView...... ......
In the DatePicker class, there is a method called the getCalendarView, and it is the method you can call if you want to get your hands on the contained CalendarView.
Once you get your hands on the contained CalendarView, then, needlessly to say, you can call its setOnDateChangeListener
像这样的东西:
Something like this:
调用
DatePicker
对象上的init()
。。Call
init()
on theDatePicker
object.