我是Android的新手,我正在开发基于位置的提醒的第一个应用程序,我在这里添加了位置和日期和时间的任务,当我到达时我应该得到通知。在添加任务活动中,我有任务名称,位置,日期和时间按钮(当显示单击DatePickerDialog和TimePickerDialog时)和AddTask按钮。 当我给出所有值并按下AddTask按钮时,我想将DatePickerDialog和TimePickerDialog重置为当前日期和时间。 我的问题是它显示了我最近为任务设置的值。如何更新Dialog的.. plzz帮助..
@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(" ");
}
答案 0 :(得分:0)
您应该保存显示的对话框实例,以便日后访问:
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;
}
然后您可以像这样更改显示的日期/时间:
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);
}
};