Android - OnDateChangedListener - 如何设置?

发布于 2024-08-17 18:54:40 字数 490 浏览 10 评论 0原文

Android 中有一个名为 DatePicker.OnDateChangedListener 的事件监听器。 我正在尝试设置 DatePicker 视图的日期更改侦听器,如下所示:

DatePicker dp = new DatePicker(getContext());
dp.setOnDateChangedListener(this); 
//where this is my activity extends DatePicker.OnDateChangedListener

但是你猜怎么着? 日期选择器没有名为 setOnDateChangedListener 的方法。

我的问题是:

  1. 那么如何在 Android 中设置日期更改侦听器?
  2. 如果无法设置日期更改监听器,那么此事件的目的是什么?

任何文档/教程都会非常有帮助。

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:

  1. How then do you set a date changed listener in Android?
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

安穩 2024-08-24 18:54:40

创建 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()

生死何惧 2024-08-24 18:54:40

最好的办法是

        DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {

                @Override
                public void onDateChanged(DatePicker datePicker, int year, int month, int dayOfMonth) {
                    Log.d("Date", "Year=" + year + " Month=" + (month + 1) + " day=" + dayOfMonth);

                }
            });

Best way is

        DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {

                @Override
                public void onDateChanged(DatePicker datePicker, int year, int month, int dayOfMonth) {
                    Log.d("Date", "Year=" + year + " Month=" + (month + 1) + " day=" + dayOfMonth);

                }
            });
烟花易冷人易散 2024-08-24 18:54:40

这个视图实际上是四个视图的组合,它们是:

三个 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

一梦等七年七年为一梦 2024-08-24 18:54:40

像这样的东西:

DatePicker myDatePicker = (DatePicker) findViewById(R.id.my_date_picker);
myDatePicker.getCalendarView().setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
    @Override
    public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
        Log.d("tag", "finally found the listener, the date is: year " + year + ", month "  + month + ", dayOfMonth " + dayOfMonth);
    }
});

Something like this:

DatePicker myDatePicker = (DatePicker) findViewById(R.id.my_date_picker);
myDatePicker.getCalendarView().setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
    @Override
    public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
        Log.d("tag", "finally found the listener, the date is: year " + year + ", month "  + month + ", dayOfMonth " + dayOfMonth);
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文