如何检测日期选择器对话框的取消点击?
Posted
技术标签:
【中文标题】如何检测日期选择器对话框的取消点击?【英文标题】:How do I detect a cancel click of the datepicker dialog? 【发布时间】:2011-02-25 02:43:28 【问题描述】:我正在使用以下 日期选择器示例
http://developer.android.com/guide/tutorials/views/hello-datepicker.html
现在我想在单击日期选择器取消按钮时执行一些功能,但在 datepickerdialog 中看不到取消事件
谁能指导我如何做到这一点。
任何帮助将不胜感激。
【问题讨论】:
【参考方案1】:我是这样做的:
DatePickerDialog dialog = new DatePickerDialog(this,
mDateSetListener,
year, month, day);
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.cancel), new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog, int which)
if (which == DialogInterface.BUTTON_NEGATIVE)
// Do Stuff
);
【讨论】:
正是我想要的,我想设置一个 onClick,有趣的是你必须覆盖默认按钮 出现错误:方法 setButton(int, String, new DialogInterface.OnClickListener()) 未定义 DatePickerActivity.DatePickerFragment 类型 这就是我想要的。谢谢你。 :) 喜显示取消按钮,但点击该按钮不起作用 现在你能告诉我如何在我的 onclick 按钮中知道用户设置的日期吗?我的意思是在上面的函数中我想要 yy, mm, dd 因为我会在我的编辑文本中再次设置它们??【参考方案2】:如果您想根据用户是否选择日期执行不同的操作,您可以使用 onDismiss 处理程序。请务必设置一个布尔值(例如,“isDataSet”)来指示用户是否选择了日期。这是一个例子:
// Date Picker Dialog
public void showDatePickerDialog(View view)
int year, month, day;
isDataSet = false; // this is used by the onDismiss handler
// Set initial time period in DatePicker to current month
calendarCurrent = Calendar.getInstance();
month = calendarCurrent.get(Calendar.MONTH);
day = calendarCurrent.get(Calendar.DAY_OF_MONTH);
year = calendarCurrent.get(Calendar.YEAR);
DatePickerDialog datePickerDialog = new DatePickerDialog(YourActivity.this, dateSetListener, year, month, day );
datePickerDialog.setOnDismissListener(mOnDismissListener);
datePickerDialog.show();
datePickerDialog_visible=true; //indicate dialog is up
// [END showDatePickerDialog]
//onDismiss handler
private DialogInterface.OnDismissListener mOnDismissListener =
new DialogInterface.OnDismissListener()
public void onDismiss(DialogInterface dialog)
datePickerDialog_visible=false; //indicate dialog is cancelled/gone
if (isDataSet) // [IF date was picked
//do something, date now selected
else
//do someething else, dialog cancelled or exited
;
【讨论】:
【参考方案3】:由于DatePickerDialog
是Dialog
,它支持以下内容:
setOnCancelListener(OnCancelListener listener)
setOnDismissListener(OnDismissListener listener)
使用这两个侦听器,您可以识别日期选择器对话框的所有解除。第一个回答问题,后者完成。
【讨论】:
【参考方案4】:DatePickerDialog dialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener()
@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2)
, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
dialog.setOnCancelListener(new DialogInterface.OnCancelListener()
@Override
public void onCancel(DialogInterface dialogInterface)
);
dialog.show();
【讨论】:
【参考方案5】:DatePickerDialog 现在公开了一个 onCancel 监听器。
DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(new DatePickerDialog.OnDateSetListener()
@Override
public void onDateSet(DatePickerDialog datePickerDialog, int year, int month, int day)
// Do whatever you want when the date is selected.
editText.setText(String.format("%04d-%02d-%02d", year, month+1, day));
, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.setOnCancelListener(new DialogInterface.OnCancelListener()
@Override
public void onCancel(DialogInterface dialog)
);
【讨论】:
【参考方案6】:我向@PeteH 很好的答案添加了一些额外的代码(它提供了对用户按下日期或取消的反应)。
private void showDatePickerDialog()
final int[] year = new int[1];
final int[] month = new int[1];
final int[] day = new int[1];
isDataSet = false; // this is used by the onDismiss handler
// Set initial time period in DatePicker to current month
Calendar calendarCurrent = Calendar.getInstance();
month[0] = calendarCurrent.get(Calendar.MONTH);
day[0] = calendarCurrent.get(Calendar.DAY_OF_MONTH);
year[0] = calendarCurrent.get(Calendar.YEAR);
DatePickerDialog datePickerDialog = new DatePickerDialog(
MyActivity,
new DatePickerDialog.OnDateSetListener()
@Override
public void onDateSet(DatePicker datePicker, int y, int m, int d)
isDataSet =true;
//user confirms date
year[0] =y;
month[0] = m;
day[0] =d;
//do something with date selected
,
year[0],
month[0],
day[0]
);
datePickerDialog.setOnDismissListener(mOnDismissListener);
datePickerDialog.show();
private DialogInterface.OnDismissListener mOnDismissListener =new DialogInterface.OnDismissListener()
public void onDismiss(DialogInterface dialog)
if (!isDataSet)
//do something if user cancels
;
【讨论】:
【参考方案7】:在这部分代码中:
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener()
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth)
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
;
你还必须实现onClick():
public void onClick (DialogInterface dialog, int which)
if (which == Button.NEGATIVE)
//Cancel
Button.NEGATIVE 的 API
【讨论】:
它从不调用 onClick 事件。【参考方案8】:以下代码帮助我解决了取消按钮未关闭日期选择器窗口的问题。感谢 eSilver 提供的解决方案。
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog, int which)
if (which == DialogInterface.BUTTON_NEGATIVE)
datePickerPlugin.success(new PluginResult(PluginResult.Status.OK, ""), callBackId);
dateDialog.hide();
);
【讨论】:
现在你能告诉我如何在我的 onclick 按钮中知道用户设置的日期吗?我的意思是在上面的函数中我想要 yy, mm, dd 因为我会在我的编辑文本中再次设置它们??【参考方案9】:很抱歉弹出了一个古老的问题,但我遇到了类似的问题,并以适当的行为编写了标准 DatePickerDialog 的简单继任者。看到它in this topic.我希望这会有所帮助。
【讨论】:
【参考方案10】:public void showDatePicker()
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);
DatePickerDialog datePicker = new DatePickerDialog(getActivity(), (view, year1, month1, day1) ->
Calendar c1 = Calendar.getInstance();
c1.set(year1, month1, day1);
Date date = c1.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("dd MMMM yyyy");
String d = formatter.format(date);
// btnDatePicker.setText(d);
// selectedDate = d;
, year, month, day)
@Override
public void onClick(@NonNull DialogInterface dialog, int which)
super.onClick(dialog, which);
if (which == DialogInterface.BUTTON_POSITIVE)
else if (which == DialogInterface.BUTTON_POSITIVE)
;
datePicker.getDatePicker().setMinDate(System.currentTimeMillis());
datePicker.show();
【讨论】:
【参考方案11】:简单地说,
如果您正在使用或扩展AppCompatDialogFragment()
只需覆盖
override fun onCancel(dialog: DialogInterface)
super.onCancel(dialog)
// write your own code
就是这样:)
【讨论】:
【参考方案12】:试试这个:
if (resultCode == RESULT_CANCELED)
Toast toast = Toast.makeText(this,"Datepicker Canceled", 10000);
toast.show();
return;
【讨论】:
我应该在什么情况下使用它? 您将在 Click 上触发 Datepicker 活动,因此在公共 void onActivityResult() 事件中,您可以调用此代码 public void onActivityResult() if (resultCode == RESULT_CANCELED) Toast toast = Toast .makeText(this,"日期选择器取消", 10000); toast.show();返回; 不知道这是怎么回事 - 没有 DatePickerActivity,它是一个对话框,所以它保留在活动中。以上是关于如何检测日期选择器对话框的取消点击?的主要内容,如果未能解决你的问题,请参考以下文章