Android 确认对话框返回 true 或 false [英] Android Confirmation dialog returning true or false

查看:19
本文介绍了Android 确认对话框返回 true 或 false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎没有简单的方法让警报对话框返回一个简单的值.
此代码不起作用(answer 变量无法从侦听器中设置,实际上它甚至无法编译)

It seems to be there is no easy way to get an Alert dialog to return a simple value.
This code does not work (the answer variable cannot be set from within the listener, in fact it does not even compile)

public static boolean Confirm(Context context) {
    boolean answer;
    AlertDialog dialog = new AlertDialog.Builder(context).create();
    dialog.setTitle("Confirmation");
    dialog.setMessage("Choose Yes or No");
    dialog.setCancelable(false);
    dialog.setButton(DialogInterface.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int buttonId) {
            answer = true;
        }
    });
    dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int buttonId) {
            answer = false;
        }
    });
    dialog.setIcon(android.R.drawable.ic_dialog_alert);
    dialog.show();
    return answer;
}

注意:方法是自包含的,即它不依赖于变量或外部构造,这一点很重要.只需调用它并得到你的答案,无论是真是假.

NOTE: It is important that the method is self contained, i.e., it does not depend on variables or constructs external to it. Just call it and get your answer, true or false.

那么,该怎么办?这个返回 truefalse 的简单愿望似乎比它应得的要复杂得多.

So, what to do? This simple wish of returning true or false seems to be much more complicated than it deserves.

另外,setButton 方法具有以下形式:

Also, the setButton method has the form:

dialog.setButton(int buttonId, String buttonText, Message msg)

但不清楚如何使用,消息发送到哪里,发给谁,使用哪个处理程序?

But it is not clear how to use it, where is the meesage sent to, to whom, which handler is used?

推荐答案

好吧,我要说我对自己很满意,因为我自己找到了一个简单的答案!
但事实是,虽然我找到了一种返回值的方法(如下所示).没有用.

Well, I was going to say that I am very pleased with myself because I found a simple answer, all by myself!
But the truth is that although I find a way to return a value (which I show below). It is of no use.

真正的问题是我想要一个同步对话框,该对话框在 dialog.show() 之后恢复您的代码之前等待用户回答.
Android 中没有这样的野兽.所有对话框都是异步的,所以 dialog.show() 只在某个队列中发布对话框(我认为)并继续.因此,您无法及时得到答案.

The real problem is I wanted a synchronous Dialog, a dialog that waits for the user to answer before resuming your code after dialog.show().
There is no such beast in Android. All dialogs are asynchronous, so dialog.show() only posts the dialog in some queue (I think) and continues. Thus you don't get your answer in time.

尽管它的所有价值(没有),您将在下面找到如何在构建对话框的方法中设置一个值.也许这种技术还有其他用途,与对话生命周期无关.

For all its worth (nothing) below you'll find how to set a value inside the method that builds the dialog. Maybe there are other uses for this technique, not related to the dialog lifecycle.







为了提供一些相关信息,我会说如果你替换

To give some related info, I'll say that if you replace

boolean answer;

final boolean answer;

可以从侦听器中访问该变量,但不能为其分配新值,因为它被声明为 final.

it is possible to access the variable from within the listener, but it is not possible to assign it a new value, since it was declared as final.

诀窍来了.
将变量定义为:

Here comes the trick.
Define the variable as:

final boolean[] answer = new boolean[1];

你们中的一些人已经明白为什么这会奏效.这里的最终变量不是布尔数组的单个元素,而是数组本身.
所以现在您可以根据需要分配数组元素 [0].

Some of you already see why this will work. The final variable here is not the single element of the boolean array, is the array itself.
So now you can assign the array element [0] as you wish.

dialog.setButton(DialogInterface.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int buttonId) {
        answer[0] = true;
    }
});
. . .
return answer[0];

最后你可以从你的方法中返回它.

And finally you can return it from your method.

这篇关于Android 确认对话框返回 true 或 false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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