1
通过在API演示中引用DateWidgets1.java
,我倾向于通过以下方式显示日期选取器小部件。只有在按下完成按钮时才获取DatePickerDialog的值
private void initDateTextView() {
dateTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
showDialog(DATE_DIALOG_ID);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
return new DatePickerDialog(this, datePickerListener,
this.year, this.month, this.date);
}
return null;
}
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case DATE_DIALOG_ID:
((DatePickerDialog)dialog).updateDate(this.year, this.month, this.date);
}
}
private static final int DATE_DIALOG_ID = 0;
private final 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) {
Log.i("CHEOK", selectedYear + ", " + selectedMonth + ", " + selectedDay);
}
};
但是,我知道,onDateSet将永远被调用,以最新的微调选择的日期,无论我按对话框的完成按钮,或者按电话上的回软按钮。
我只对获取所选日期感兴趣,当用户按完成按钮,但没有后退按钮。
有什么办法可以实现吗?