【问题标题】:DatePickerDialog listenerDatePickerDialog 监听器
【发布时间】:2015-03-26 08:01:37
【问题描述】:

我正在制作一个使用DatePickerDialog 的Android 应用程序。预期的功能是让用户在对话框中选择一个日期,然后让TextView 反映所选日期。但是,当用户选择日期时,我找不到任何类型的点击监听器来通知我的活动。我正在寻找一种方法来检测用户何时选择了日期,但来自我的主要活动。这是我的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 的类启动对话框,并且我正在寻找一种方法来检测用户是否从对话框中选择了日期。有什么建议吗?

【问题讨论】:

  • 这可以通过定义一个公共函数来实现
  • 如果您更改的日期将被粘贴到文本视图上,那么您应该简单地覆盖 onTextChanged 以检查日期是否已更改

标签: android listener android-datepicker


【解决方案1】:

我推荐阅读: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
    }
}

如果您将以下代码添加到DialogPickerFragment 类,则用户选择日期时将通知Activity

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

【讨论】:

  • 小记:onAttach(Activity activity)现在已经弃用了,最好使用onAttach(Context context)
【解决方案2】:

最简单的方法..

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-14
    • 2013-12-24
    • 2014-12-13
    • 2014-02-21
    • 1970-01-01
    • 2019-06-06
    • 2021-07-02
    • 1970-01-01
    相关资源
    最近更新 更多