DatePickerDialog 侦听器

DatePickerDialog listener

我正在制作一个 Android 应用程序,其中我使用 DatePickerDialog。预期的功能是让用户在对话框中 select 一个日期,然后 TextView 来反映所选日期。但是,当用户 select 编辑了日期时,我找不到任何类型的点击侦听器来通知我的 Activity。我正在寻找一种方法来检测用户何时 select 约会,但来自我的主要 Activity。这是我的 DatePickerDialog class:

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

private GregorianCalendar date;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current date as the default date in the picker
    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);

    // Create a new instance of DatePickerDialog and return it
    return new DatePickerDialog(getActivity(), this, year, month, day);
}

@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
    date = new GregorianCalendar(year, month, day);
}

public GregorianCalendar getDate() {
    return date;
}
}    

以及我启动对话框的代码:

DialogFragment datePicker = new DatePickerFragment();
datePicker.show(getSupportFragmentManager(), "datePicker");

我目前从扩展 Activity 的 class 启动对话框,我正在寻找一种方法来从 class 中检测用户是否有 select 从对话框中输入日期。有什么建议吗?

最简单的方法..

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

import java.util.Calendar;

public class MyDialogFragment extends DialogFragment implements     DatePickerDialog.OnDateSetListener {

OnDateSetListener mDateSetListener;


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Calendar calendar = Calendar.getInstance();
    int yy = calendar.get(Calendar.YEAR);
    int mm = calendar.get(Calendar.MONTH);
    int dd = calendar.get(Calendar.DAY_OF_MONTH);
    return new DatePickerDialog(getActivity(), this, yy, mm, dd);
}


@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    if (this.mDateSetListener != null) {
        this.mDateSetListener.onDateSet(view, year, monthOfYear, dayOfMonth);
    }
}

public void setOnDateSetListener(OnDateSetListener dateSetListener) {
    this.mDateSetListener = dateSetListener;
}

public interface OnDateSetListener {
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth);
}

}

在您的 activity 中执行此操作..

implement MyDialogFragment.OnDateSetListener;

MyDialogFragment myDialogFragment = new MyDialogFragment();
            myDialogFragment.show(getFragmentManager(), "DatePicker");
            myDialogFragment.setOnDateSetListener(this);

@Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
       //Here is your data..
    }

我推荐阅读:Communicating with Other Fragments

Activity 必须实现 DatePickerDialog.OnDateSetListener 接口:

public class MyActivity extends ActionBarActivity implements DatePickerDialog.OnDateSetListener {
    ...

    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        // the user selected, now you can do whatever you want with it
    }
}

Activity 将在用户选择日期时收到通知,如果您将以下代码添加到 DialogPickerFragment class:

public class DatePickerFragment extends DialogFragment {
    private DatePickerDialog.OnDateSetListener mListener;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        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);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), mListener, year, month, day);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        if (!(context instanceof DatePickerDialog.OnDateSetListener)) {
            throw new IllegalStateException("Activity must implement fragment's callbacks.");
        }

        mListener = (DatePickerDialog.OnDateSetListener) activity;
    }

    @Override
    public void onDetach() {
        super.onDetach();

        mListener = null;
    }
}