android怎么确定对话框返回的值是true还是false?

似乎没有简单的方法获得一个警告框来返回一个简单的值。
下边的代码不起作用(答案变量不能设置在监听器里,事实上它甚至都没有编译)

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;
}

注意:很重要的是这个方法是自己包含的,也就是说,它不依赖于外部变量或者是构造。仅仅是调用它然后获得你的值,true或者是false。

所以,应该怎么做?简单的想法就是返回true或者是false,但是这似乎比它自己来获得这些值复杂得多。
而且,setButton有这个表单:

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

但是不知道怎么用到它,这个消息应该发到哪,发给谁,哪个处理器是被用到的?

我想说我对我自己的答案很满意,因为完全是靠我自己找到了一个简单的方法。
但是事实是尽管我发现了返回一个值的方法(就是下边的),但是它不起作用。
真正的问题是我想要一个同步的对话框,对话框在恢复代码之前在dialog.show()之后等待用户来回答
在android中没有这样的beast。所有的对话框都是异步的,所以dialog.show()只是在一些队列中贴了这个对话框然后继续。因此不能及时的得到答案。
对于所有的它的价值(没什么)下面你会在创建一个对话框的方法里面找到怎么设置一个值。也许这种技术还有其他的用途,不仅仅是在对话框的生命周期中。

下边是一些相关信息,如果你用final boolean answer;替换boolean answer;我可能会说:它可能会在监听器里边访问内部变量,但是不可能为它分配一个新值,因为它是已经被声明过了的了。=
这里关键的问题出现了。
定义变量为

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

一些人已经知道为什么这个将起作用了。这里的最后变量不是的单一的布尔数组,而是该数组本身。
所以现在你可以指定数组element [0]

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

最终你能从你的方法中返回它。

你能做的就是用Interface给你的Alert Dialog创建一个监听器用来监听AlertDialogs的动作
创建Interface

public class MyInterface {

    DialogReturn dialogReturn;

    public interface DialogReturn {

        void onDialogCompleted(boolean answer);
    }

    public void setListener(DialogReturn dialogReturn) {
        this.dialogReturn = dialogReturn;
    }

    public DialogReturn getListener() {
        return dialogReturn;

    }
}

现在,用你创建的 implements MyInterface.DialogReturn在你的类只是实现了Interface
然后你可以设置监听器而且能让它想下边的这样作用,

public class Main extends Activity implements MyInterface.DialogReturn{

    MyInterface myInterface;
    MyInterface.DialogReturn dialogReturn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
                ....
        myInterface = new MyInterface();
        myInterface.setListener(this);
    }


   public void Confirm(Context context) {
        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) {
                myInterface.getListener().onDialogCompleted(true);
            }
        });
        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int buttonId) {
                myInterface.getListener().onDialogCompleted(false);
            }
        });
        dialog.setIcon(android.R.drawable.ic_dialog_alert);
        dialog.show();
         }


@Override
    public void onDialogCompleted(boolean answer) {
        Toast.makeText(Main.this, answer+"", Toast.LENGTH_LONG).show();
            if(answer)
            // do something
            else
            // do something
    }
}

这是我之前遇到问题的答案。我之前遇到的问题是怎么创建一个分开的确认框,可以通过其他的类或者activity访问,所以在那个确认框类中我们不需要写长代码。
首先你必须创建一个DialogHandler.java

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import src.app.R;

public class DialogHandler {
    public Runnable ans_true = null;
    public Runnable ans_false = null;

    // Dialog. --------------------------------------------------------------

    public boolean Confirm(Activity act, String Title, String ConfirmText,
            String CancelBtn, String OkBtn, Runnable aProcedure, Runnable bProcedure) {
        ans_true = aProcedure;
        ans_false= bProcedure;
        AlertDialog dialog = new AlertDialog.Builder(act).create();
        dialog.setTitle(Title);
        dialog.setMessage(ConfirmText);
        dialog.setCancelable(false);
        dialog.setButton(DialogInterface.BUTTON_POSITIVE, OkBtn,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int buttonId) {
                         ans_true.run();
                    }
                });
        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, CancelBtn,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int buttonId) {
                        ans_false.run();
                    }
                });
        dialog.setIcon(android.R.drawable.ic_dialog_alert);
        dialog.show();
        return true;
    }
}

这是一个在另外一个类中调用它的示例

public class YourActivity extends Activity {
    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViewById(R.id.button1).setOnClickListener(myclick);
    }

    public final Button.OnClickListener myclick = new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            doclick();
        }
    };

    public void doclick() {
        DialogHandler appdialog = new DialogHandler();
        boolean dlg = appdialog.Confirm(this, "Message title", "Message content",
                "Cancel", "OK", aproc(), bproc());
    }

    public Runnable aproc(){
        return new Runnable() {
            public void run() {
                Log.d("Test", "This from A proc");
            }
          };
    }

    public Runnable bproc(){
        return new Runnable() {
            public void run() {
                Log.d("Test", "This from B proc");
            }
          };
    }


}