我像这样实现了这个自定义 AlertDialog:
AlertDialog dialog = new AlertDialog.Builder(this)
.setView(dialogView)
.setTitle(getResources().getString(R.string.addedit_asklater_price))
.setCancelable(false)
.create();
dialog.setMessage(text);
dialog.setOwnerActivity(this);
dialog.setButton(...)
dialog.setButton(...)
dialog.show();
doProcessUserInput();
但是,我注意到在 dialog.show()
控件立即流向 doProcessUserInput()
之后,无需等待用户使用任何对话框关闭对话框纽扣。
这种行为看起来很奇怪,我希望对话框是模态的,就像我一直知道的模态对话框一样。
我可以重组我的代码,以便从对话框按钮的 onClickListener
调用 doProcessUserInput()
。然而,我想知道是否有一种方法可以在 dialog.show()
处暂停程序执行,直到对话框完成。
附言:
- 我试过使用扩展 Dialog 的 Custom Dialog,但它有
同样的问题。我在按钮中创建对话框
onClickListener
。 - 我已经尝试实现
Activity::onCreateDialog
并使用showDialog(id)
而不是dialog.show()
,但是有同样的问题。
最佳答案
当然,您可以为您的对话框设置onPositiveButton
点击监听器,并从该监听器执行您的操作。
如果您确实想在某个点暂停您的 Activity ,您可能可以使用旧的 java wait/notify
机制或更方便的新机制,例如执行程序。
你为什么要这样做还不清楚。 Android 对话框专门设计为非模态对话框,因此它们绝不会阻止您的应用程序(因为您的应用程序可以包含其他 Activity ,例如 native 电话调用 Activity ,如果这些 Activity 被阻止,那就太糟糕了)。
关于android - 如何使android对话框模态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8382728/