Android - OnDateChangedListener - 你如何设置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2051153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:22:52  来源:igfitidea点击:

Android - OnDateChangedListener - how do you set this?

androidandroid-datepicker

提问by Tawani

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:

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

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.

但猜猜怎么了?日期选择器没有名为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?
  1. 那么如何在 Android 中设置更改日期的侦听器?
  2. 如果无法设置更改日期的侦听器,此事件的目的是什么?

Any documentation/tutorials will be very helpful.

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

回答by Christopher Orr

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.

一旦你创建了你的DatePicker,你需要用你想要显示的日期初始化它。这就是您可以添加listener 的地方

See DatePicker.init(int, int, int, OnDateChangedListener).

DatePicker.init(int, int, int, OnDateChangedListener)

回答by turbandroid

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);

                }
            });

回答by user2389347

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...... ......

从OnDateChangeListener开始,你传入init方法的对象会简单的传给包含的CalendarView,相信你也知道老式的CalendarView里面有个setOnDateChangeListener方法...... ..

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.

在 DatePicker 类中,有一个名为 getCalendarView 的方法,如果您想使用所包含的 CalendarView,您可以调用该方法。

Once you get your hands on the contained CalendarView, then, needlessly to say, you can call its setOnDateChangeListener

一旦你掌握了包含的 CalendarView,那么不用说,你可以调用它的 setOnDateChangeListener

回答by Andrew

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);
    }
});