DatePickerDialog侦听器

时间:2015-03-26 08:01:37

标签: android listener android-datepicker

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

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的类启动对话框,我正在寻找一种从该类中检测用户是否从对话框中选择了日期的方法。有什么建议吗?

2 个答案:

答案 0 :(得分:2)

我建议阅读: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

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

答案 1 :(得分:1)

最简单的方式..

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

}

在你的活动中这样做..

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