Android DataPickerDialog onDateChanged未触发

时间:2012-08-09 15:13:11

标签: java android

我在使用Android教程后的步骤中遇到了DatePickerDialog的问题。

链接TextView以在获得焦点时启动DatePickerDialog,如下所示:

EditText fNac = (EditText)findViewById(R.id.regFecha);
            fNac.setonfocusChangeListener(new View.onfocusChangeListener(){

                    @Override
                    public void onfocusChange(View v, boolean hasFocus) {
                            // TODO Auto-generated method stub
                            // Desde aquí lanzamos el datepicker
                            if (hasFocus){
                                    DialogFragment newFragment = new DatePickerFragment();
                                    newFragment.show(getSupportFragmentManager() , "datePicker");
                            }
                    }

工作正常,启动DatePickerDialog。我实例化了一个以这种方式创建的新DatePickerFragment类:

import java.util.Calendar;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.widget.DatePicker;

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    private DatePickerDialog mDatePickerDialog;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){
            // Usamos la fecha actual como primera fecha a mostrar
            final Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);

            // Devolvemos la instancia de un Dialog con la fecha actual
            mDatePickerDialog = new DatePickerDialog(getActivity(),this,year,month,day);
            return mDatePickerDialog;
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int day) {
            // TODO Auto-generated method stub
            Log.v("LOG_CAT","OK");
    }

    public void onDateChanged(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
            // TODO Auto-generated method stub
            Log.v("LOG_CAT", "Ok2");
    }

}

这就是问题所在。我想使用onDateChanged方法更改Dialog的标题,但它从未调用过。我试图创建一个onDateChangedListener,但DatePickerDialog没有。

我该怎么做?

提前致谢。

3 个答案:

答案 0 :(得分:6)

只需扩展DatePickerDialog类并覆盖onDateChanged,如下所示。我包含了一个新的构造函数,它接受一个Calendar对象并设置年,月和日本身,为您节省了不必要的输入。 (我不明白为什么这不是一个选项。)无论如何这里是如何使用类和更短的构造函数:

public class DatePickerFragment extends DialogFragment implements OnDateSetListener {
    public class MyDatePickerDialog extends DatePickerDialog {
        private Calendar calendar;
        private final String format = "EEEE, MMMM dd, yyyy";

        // Regular constructor
        public MyDatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {
            super(context, callBack, year, monthOfYear, dayOfMonth);
            calendar = Calendar.getInstance();
        }

        // Short constructor
        public MyDatePickerDialog(Context context, OnDateSetListener callBack, Calendar calendar) {
            super(context, callBack, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
            this.calendar = calendar;
        }

        @Override
        public void onDateChanged(DatePicker view, int year, int month, int day) {
            super.onDateChanged(view, year, month, day);
            calendar.set(year, month, day);
            setTitle(DateFormat.format(format, calendar));
        }
    }

    private MyDatePickerDialog mDatePickerDialog;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDatePickerDialog = new MyDatePickerDialog(this, this, Calendar.getInstance());
        return mDatePickerDialog;
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int day) {
        Log.v("DatePickerFragment", "onDateSet");
    }
}

答案 1 :(得分:0)

我有解决方案,只需在对话框中获取DatePicker并设置init方法

这是一个例子:

  

android.widget.DatePicker datePicker = mDatePickerDialog.getDatePicker();

并像这样设置init

  

datePicker.init(int YEAR,int MONTH,int Day,OnDateChangedListener)

希望这个帮助

答案 2 :(得分:-2)

也许你应该说

public class DatePickerFragment extends DialogFragment 
    implements DatePickerDialog.OnDateSetListener, DatePicker.OnDateChangedListener {
    ...
}