Android 嵌套 AlertDialog - 这可能吗?
在对数据库执行不可逆转的操作之前,我试图要求用户确认两次。问题在于外部单击处理程序不等待内部单击处理程序。单击第一个对话框上的“是”按钮后,会短暂显示第二个对话框,但外部处理程序仍然执行并完成,最终销毁两个对话框。
new AlertDialog.Builder(ActivityMain.this).setMessage(
"Are you sure?").setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
new AlertDialog.Builder(ActivityMain.this).setMessage(
"Are you really sure?").setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
....
这是为什么?
I am trying to ask the user for confirmation twice before I do something irreversible to the database. The problem is that the outer click handler does not wait for the inner click handler. Once the Yes button is clicked on the first dialog, the 2nd dialog is displayed briefly, but the outer handler executes and completes nonetheless, ultimately destroying both dialogs.
new AlertDialog.Builder(ActivityMain.this).setMessage(
"Are you sure?").setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
new AlertDialog.Builder(ActivityMain.this).setMessage(
"Are you really sure?").setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
....
Why is that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需设计一个新的 xml 布局作为您的对话框并创建一个新活动,并将其主题设置为活动标记下的清单文件中的 @android:style/Theme.Dialog:
在对话框中单击侦听器代码启动活动,
如下所示您的新活动作为一个对话框,您可以在其中轻松应用您的操作。
Just Design a new xml layout as your dialog and create a new activity and set it's theme to @android:style/Theme.Dialog in the manifest file under the activity tag ex:
in the Dialog click listener code start the activity as
This will start your new activity as a dialog where you can apply your action easily.
我相信这是因为对话框没有被阻止。一旦它们显示出来,处理就会转移到下一行代码。但该对话框仍然显示,等待用户交互。
I believe it's because dialogs are not blocking. As soon as they are displayed, processing moves on to the next line of code. The dialog is still displayed though, awaiting user interaction.
另一种方法是关闭第一个单击处理程序中的第一个对话框,
然后添加
第二个对话框的实例化
Another way is to close the first dialog in the first click handler with
then add
in your instantiation of the second dialog