Android 更新对话框
我的活动有一个文本视图,用于显示日期。当用户单击TextView时,我会启动DatePickerDialog,如下所示:Android 更新对话框,android,datepickerdialog,Android,Datepickerdialog,我的活动有一个文本视图,用于显示日期。当用户单击TextView时,我会启动DatePickerDialog,如下所示: public void onClick(View v) { if (v.getId() == R.id.date_wrapper) { showDialog(DATE_DIALOG_ID); } } @Override protected Dialog onCreateDialog(int id) { switch (id) {
public void onClick(View v) {
if (v.getId() == R.id.date_wrapper) {
showDialog(DATE_DIALOG_ID);
}
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
GregorianCalendar date = new GregorianCalendar();
if (mData != null) {
date.setTimeInMillis(mData.getDate());
}
return new DatePickerDialog(this, datePickerListener, date.get(Calendar.YEAR), date.get(Calendar.MONTH), date.get(Calendar.DAY_OF_MONTH));
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
GregorianCalendar selectedDate = new GregorianCalendar();
selectedDate.set(Calendar.YEAR, selectedYear);
selectedDate.set(Calendar.MONTH, selectedMonth);
selectedDate.set(Calendar.DAY_OF_MONTH, selectedDay);
mData.setDate(selectedDate.getTimeInMillis());
populateDate();
}
};
这很有效。但是,当用户单击提交按钮时,我想将日期设置回今天。我可以轻松地将mData对象的日期变量设置为今天。但是,我不确定如何更新DatePickerDialog。它已经创建,因此单击TextView不会再次运行onCreateDialog。因此,当我单击TextView时,DatePickerDialog打开,这是我选择的最后一个日期
如何引用DatePickerDialog来更新日期?删除DatePickerDialog也是可以接受的。我找到了一个可以接受的解决方案
当我重置日期时,我还应该调用
removeDialog(date\u DIALOG\u ID)代码>以销毁对话框。下一次showDialog(日期对话框\u ID)调用code>时,将重新创建它。为什么不在用户每次单击文本视图时运行一个新的datapickerdialog实例?通过这种方式总是调用createDialog,对吗?如何调用?我只是简单地调用showDialog(我的ID)代码>哪种是系统方法可以发布活动代码?所以我可以看到你在哪里以及如何调用每个部分的对话框。我已经更新了OP。所有相关的代码都发布了。