将 DatePickerDialog 和 TimePickerDialog 更新为当前日期和时间

Update DatePickerDialog and TimePickerDialog to Current date and time

我是 android 的新手,我正在开发我的第一个基于位置的提醒应用程序,我在其中添加带有位置、日期和时间的任务,当我到达时我应该收到通知。在添加任务 activity 中,我有任务名称、位置、日期和时间按钮(单击 DatePickerDialog 并显示 TimePickerDialog 时)和一个 AddTask 按钮。 当我提供所有值并按下 AddTask 按钮时,我想将 DatePickerDialog 和 TimePickerDialog 重置为当前日期和时间。 我的问题是它显示了我最近为任务设置的值。如何更新对话框的.. plzz help..

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_task);
    init();

    /** Capture our View elements */
    //pDisplayDate = (TextView) findViewById(R.id.displayDate);
    pPickDate = (Button) findViewById(R.id.pickDate);

    /** Listener for click event of the button */
    pPickDate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
        }
    });

   /** Capture our View elements */
    //pDisplayTime = (TextView) findViewById(R.id.displayTime);
    pPickTime = (Button) findViewById(R.id.pickTime);


    /** Listener for click event of the button */
    pPickTime.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(TIME_DIALOG_ID);
        }
    });

    /** Get the current date and time */
    final Calendar cal = Calendar.getInstance();
    pYear = cal.get(Calendar.YEAR);
    pMonth = cal.get(Calendar.MONTH);
     pDay = cal.get(Calendar.DAY_OF_MONTH);

    mHour = cal.get(Calendar.HOUR_OF_DAY);
    mMinute = cal.get(Calendar.MINUTE);
}

/** Create a new dialog for date and time picker */
@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this,
                    pDateSetListener,
                    pYear, pMonth, pDay);

        case TIME_DIALOG_ID:
            return new TimePickerDialog(this,
                    mTimeListener,
                    mHour,mMinute,true);
    }
    return null;
}

public void add_task(View view) {

    Task task = new Task();
    task.setTaskNameString(activityForm.getTaskNameInput().getText()
            .toString());
    task.setTaskLocString(activityForm.getTaskLocInput());
    task.setTaskLat(activityForm.getTaskLat());
    task.setTaskLon(activityForm.getTaskLon());
    task.setDateString(activityForm.getTaskDateInput());
    task.setTimeString(activityForm.getTaskTimeInput());

    persistTask(task);

    Toast.makeText(getApplicationContext(), "SAVED=" + task.hashCode(),
            Toast.LENGTH_LONG).show();

    activityForm.getTaskNameInput().setText(" ");

}

您应该保存显示的对话框实例,以便稍后访问:

private TimePickerDialog mTPD;
private DatePickerDialog mDPD;

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case DATE_DIALOG_ID:
            mDPD = new DatePickerDialog(this,
                    pDateSetListener,
                    pYear, pMonth, pDay);
            return mDPD;

        case TIME_DIALOG_ID:
            mTPD = new TimePickerDialog(this,
                    mTimeListener,
                    mHour,mMinute,true);
            return mTPD;
    }
    return null;
}

然后您可以像这样更改显示的 date/time:

Calendar c = Calendar.getInstance();
if (mTPD != null) {
    mTPD.updateTime(c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE));
}

if (mDPD != null) {
    mDPD.updateDate(c.get(Calendar.YEAR), c.get(Calendar.MONTH),
            c.get(Calendar.DAY_OF_MONTH));
}

编辑:

要修改对话框中设置的值,您应该使用可用的侦听器(您已经设置)。这是一个示例(未测试):

TimePickerDialog.OnTimeSetListener mTimeListener = new TimePickerDialog.OnTimeSetListener() {
    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        Calendar c = Calendar.getInstance();
        int curHour = c.get(Calendar.HOUR_OF_DAY);
        int curMin = c.get(Calendar.MINUTE);
        if (hourOfDay < curHour) hourOfDay = curHour;
        if (minute < curMin) minute = curMin;

        mTPD.updateTime(hourOfDay, minute);
    }
};