对话框/ AlertDialogs:如何与QUOT;块执行"而对话是由(.NET式) [英] Dialogs / AlertDialogs: How to "block execution" while dialog is up (.NET-style)

查看:114
本文介绍了对话框/ AlertDialogs:如何与QUOT;块执行"而对话是由(.NET式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即将从.NET环境林我现在看明白对话框在Android中是如何工作的。

Coming from the .NET-environment Im am now looking to understand how Dialogs work in Android.

在.NET中,当调用的MessageBox.show(...)创建并显示一个弹出式对话框。在调用显示我可以指定哪些按钮应该在弹出可用,例如:

In .NET, when calling MessageBox.Show(...) that creates and shows a popup dialog. In the call to Show I can specify what buttons should be available in the popup, for example:

DialogResult myDialogResult = MessageBox.Show("My text here", "My caption here", MessageBoxButtons.YesNoCancel);

正如你所看到的,调用显示将返回当按钮是pssed在弹出$ P $,通知我什么按钮被点击一个的DialogResult。请注意,在.NET中,暂停执行在该行的地方调用显示(...)的制成,所以它可以返回当按钮是pressed的价值。

As you can see, the call to Show returns a DialogResult when a button is pressed in the popup, informing me what button was clicked. Note that in .NET, execution is halted at the line where the call to Show(...) is made, so it can return the value when a button is pressed.

如果我在上面的例子preSS的否的myDialogResult将等于

If I in the above example press "No" the myDialogResult will be equal to

myDialogResult == DialogResult.No

由于我找到了.NET的方式使用/创建弹出窗口非常方便和直观的,我想在Android中创建的弹出窗口过多的这种方式。

Since I find the .NET-way of using/creating popups very easy and intuitive, I would like that way of creating popups in Android too.

所以,问题是,如果有人知道如何停止执行之类的的MessageBox.show,然后返回每当按钮时pressed一个值(和对话框消失)?

So, the question is if anyone know how to "halt execution" like with the MessageBox.Show, and then return a value whenever the Button is pressed (and the dialog goes away)?

问候

修改1: 要多一点点清晰的:

EDIT 1: To be a little bit more clear:

我需要执行暂停并等待用户选择一个按钮,点击弹出。在code跟着叫,显示对话框,依赖于被点击对话框哪个按钮。

I need for the execution to halt and wait until the user has chosen a button to click in the popup. The code that follow the call to show the Dialog is dependent on what button is clicked in the Dialog.

这就是为什么我不能用什么埃里希和Alex建议,因为写作code在onclick-方法的建议下面是不是要去工作。原因是,我不能继续正常运行。让我举一个例子:

Thats why I cannot use what Erich and Alex suggest, since writing code in the onClick-methods as suggested below is not gonna work. The reason is that I cannot continue the "normal execution". Let me take an example:

让我举个例子:

int nextStep = 0; // this variable will not be reached from within the onClick-methods

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Hello!")
       .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                nextStep = 1; // *** COMPILER ERROR!! ***
            }
        })
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                nextStep = 2; // *** COMPILER ERROR!! ***
            }
        })
        .create().show();

if (nextStep == 1)
{
    // then do some damage
}
else if (nextStep == 2
    // dont do damage

如果我想执行依赖于在弹出的选择,我会以某种方式必须作出正常运行的所有变量(在这种情况下, NEXTSTEP )在onclick可用 - 方法,那听起来像是地狱我。

If I wanted the execution to be dependent on the choice in the popup, I would somehow have to make all the variables in the "normal execution" (in this case nextStep) available in the onClick-methods, and that sounds like hell to me.

编辑2

另一个明显的例子是一个弹出,询问是否要继续替换选项

Another obvious example would be a popup asking "Do you want to continue" with the options "Yes" and "No".

如果用户presses是,整个方法应中止,否则就应该继续执行。你如何解决这个很好?

If the user presses "Yes", the whole method should be aborted otherwise it should continue execution. How do you solve that nicely?

问候

推荐答案

泰德,你不想这样做,真的:)最大的原因是,如果你阻止用户界面线程要显示一个对话框,您在将阻止线程是负责拟定和处理您的对话框的事件。这意味着你的对话将是反应迟钝。你也将导致ANRS如果用户花费超过几秒钟到点击对话框

Ted, you don't want to do this, really :) The biggest reason is that if you block the UI thread while you are displaying a Dialog, you will block the thread that's in charge of drawing and handling the events of your Dialog. Which means your dialog will be unresponsive. You will also cause ANRs if the user takes more than a few seconds to click the dialog.

埃里希的回答正是你需要的。我知道这是不是你的需要的,但是,这并不重要。我们设计的Andr​​oid为prevent开发人员编写同步对话,所以你真的没有多少选择的余地。

Erich's answer is exactly what you need. I know it's not what you want, but that doesn't matter. We've designed Android to prevent developers from writing synchronous dialogs so you don't really have much of a choice.

这篇关于对话框/ AlertDialogs:如何与QUOT;块执行"而对话是由(.NET式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆