Android 如何检查是否显示对话框或显示多个相同类型的对话框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3152507/
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
How to check if dialog is displayed or display multiple dialogs of the same type?
提问by pixel
I'm managing dialogs by showDialog
/dismissDialog
/removeDialog
.
我通过管理对话框showDialog
/ dismissDialog
/ removeDialog
。
I want to:
我想要:
Display several dialogs in kind of a stack:
以堆栈的形式显示多个对话框:
a) First dialog is shown using showDialog(DIALOG_TYPE)
a) 第一个对话框显示使用 showDialog(DIALOG_TYPE)
b) Next dialogs are shown on top of the existing dialog
b) 下一个对话框显示在现有对话框的顶部
Now I'm only able to display first dialog using showDialog
and then next dialogs are ignored.
现在我只能使用显示第一个对话框showDialog
,然后忽略下一个对话框。
Display last dialog:
显示最后一个对话框:
a) First dialog is shown using showDialog(DIALOG_TYPE)
a) 第一个对话框显示使用 showDialog(DIALOG_TYPE)
b) Application checks if dialog is displayed, closes dialog (if it's displayed) and opens a new dialog.
b) 应用程序检查对话框是否显示,关闭对话框(如果显示)并打开一个新对话框。
Is there any possibility to achieve one of the above solutions?
有没有可能实现上述解决方案之一?
回答by Robby Pond
Dialog has an isShowing()method that should return if the dialog is currently visible. So you can use that to see if a dialog is showing and hide it with dismissDialog(). You just have to keep a reference to the Dialogs you create in onCreateDialog().
Dialog 有一个isShowing()方法,如果该对话框当前可见,该方法应该返回。因此,您可以使用它来查看对话框是否正在显示并使用dismissDialog() 将其隐藏。您只需要保留对您在 onCreateDialog() 中创建的对话框的引用。
回答by pixel
You can use flag to check whether dialog is opened or not and according to flag value you can do whatever you want. Like I did. I am having only one dialog but when I touch another EditText
and if my dialog is opened then it should close first and then should re-open with animation.
您可以使用标志来检查对话框是否打开,并根据标志值您可以做任何您想做的事情。就像我一样。我只有一个对话框,但是当我触摸另一个对话框时EditText
,如果我的对话框被打开,那么它应该先关闭,然后应该用动画重新打开。
Code snippet:
代码片段:
private EditText mEditText, mEditCode;
private Dialog mDialog;
private int mClicked = 0;
private boolean isShown = false;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mEditText = (EditText)findViewById(R.id.EnterValue);
mEditText.setClickable(true);
mEditText.setOnClickListener(this);
mEditCode = (EditText)findViewById(R.id.EnterCode);
mEditCode.setClickable(true);
mEditCode.setOnClickListener(this);
}
public void onClick(View nView)
{
switch (nView.getId())
{
case R.id.EnterValue:
mClicked = R.id.EnterValue;
mEditText.requestFocus();
mEditText.setFocusableInTouchMode(false);
mEditText.setEnabled(true);
mEditText.setSelection(mEditText.getText().toString().trim().length());
if(isShown)
{
mDialog.dismiss();
showInfoDialog();
}
else
{
showInfoDialog();
}
break;
case R.id.EnterCode:
mClicked = R.id.EnterCode;
mEditCode.requestFocus();
mEditCode.setFocusableInTouchMode(false);
mEditCode.setEnabled(true);
mEditCode.setSelection(mEditCode.getText().toString().trim().length());
if(isShown)
{
mDialog.dismiss();
showInfoDialog();
}
else
{
showInfoDialog();
}
break;
}
}
private boolean showInfoDialog()
{
mDialog = new Dialog(CustomKeyboardNotLikeAndroidActivity.this, R.style.PauseDialog);
mDialog.setContentView(R.layout.keyboard);
mDialog.getWindow().getAttributes().windowAnimations = R.style.PauseDialogAnimation;
mDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
mDialog.setCancelable(true);
isShown = true;
mDialog.show();
return false;
}
Try to alter this code in your way. Hope this can help you. Thanks.
尝试以您的方式更改此代码。希望这可以帮到你。谢谢。
回答by Bartek Lipinski
Dialog
implements DialogInterface
which has OnShowListener
.
Dialog
实现DialogInterface
具有OnShowListener
.
Therefore you can use code like this:
因此,您可以使用这样的代码:
Dialog dialog = new Dialog(context);
// ... set all things considering your dialog
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
// do something when your dialog is shown
}
});