在 Android 中关闭或取消对话框有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3125647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between a dialog being dismissed or canceled in Android?
提问by Aal
Like the title says, what is the difference between a dialog being dismissed or canceled in Android?
正如标题所说,在 Android 中关闭或取消对话框有什么区别?
回答by Lee
Typically, a dialog is dismissed when its job is finished and it is being removed from the screen. A dialog is canceled when the user wants to escape the dialog and presses the Back button.
通常,对话框在其工作完成并从屏幕上移除时会被解除。当用户想要退出对话框并按下后退按钮时,对话框被取消。
For example, you have a standard Yes/No dialog on the screen. If the user clicks No, then the dialog is dismissed and the value for No is returned to the caller. If instead of choosing Yes or No, the user clicks Back to escape the dialog rather than make a choice then the dialog is canceled and no value is returned to the caller.
例如,您在屏幕上有一个标准的是/否对话框。如果用户单击“否”,则关闭对话框并将“否”的值返回给调用者。如果用户没有选择 Yes 或 No,而是单击 Back 退出对话框而不是做出选择,则对话框将被取消,并且不会向调用者返回任何值。
回答by hotshot309
dismiss
is something you have to explicitly call in your code, usually to respond to a click event on a button in your Dialog
. If you prefer, you can call dismissDialog
in the Activity
, which will in turn call dismiss
on the Dialog
.
dismiss
是您必须在代码中显式调用的东西,通常是为了响应Dialog
. 如果你愿意,你可以调用dismissDialog
的Activity
,这将反过来调用dismiss
上Dialog
。
The cancel
method only executes when it is explicitly called in your code, or when the user presses the BACK button when your cancelable Dialog
is open (as @Lee noted).
该cancel
方法仅在您的代码中显式调用时执行,或者当您的可取消项Dialog
打开时用户按下 BACK 按钮(如@Lee 所述)。
If you are using a DatePicker
, then all of this is still the case. As @Lee said, DatePickerDialog.OnDateSetListener
just detects when the user has chosen a date from the DatePicker
.
如果您使用的是DatePicker
,那么所有这些仍然是这种情况。正如@Lee 所说,DatePickerDialog.OnDateSetListener
只检测用户何时从DatePicker
.
The Android Developer Referenceprovides more info on Dialog
s.
在Android开发者参考提供更多信息Dialog
秒。
回答by ucMedia
Dismiss
Calling the dismiss removes the dialog from the screen. This method can be
invoked safely from any thread. Note that you should not override this method to do cleanup when the dialog is dismissed, instead implement that in onStop.
Dismiss
调用dismiss 会从屏幕上移除对话框。可以从任何线程安全地调用此方法。请注意,当对话框关闭时,您不应覆盖此方法进行清理,而是在onStop 中实现该方法。
Cancel
Calling the cancel, cancels the dialog. This is essentially the same as calling dismiss(), but it will also call your DialogInterface.OnCancelListener, if registered.
Cancel
调用取消,取消对话框。这本质上与调用dismiss()相同,但它也会调用您的DialogInterface.OnCancelListener(如果已注册)。
Hide
This method hides the dialog, but do not dismiss it.
Hide
此方法隐藏对话框,但不要关闭它。
And for more see here
有关更多信息,请参见此处