AlertDialog在一个具有返回值的函数中

时间:2012-12-03 08:50:02

标签: android alertdialog

我编写了一个返回值为int的函数,在这个函数中我需要弹出一个带有两个按钮的AlertDialog。单击“是”按钮时,函数返回0,“否”按钮返回-1。

public int Func(){
    final AlertDialog d=new AlertDialog.Builder(mContext).setTitle("Warning").setCancelable(false).setMessage
                       (alert)
                       .setPositiveButton("Yes",mListener).setNegativeButton("No",mListener).create();
                       d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
                       d.show();
   if(mWhich.getWhich()==-1)   //the "yes" button was clicked
       return 0;
   else                        //the "no"  button was clicked
       return -1;
}

mWhich是用于记录用户选择的类

private DialogInterface.OnClickListener mListener =
            new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    mWhich.setWhich(which);
                             }
            };

现在问题是代码

if(mWhich.getWhich()==-1)
       return 0;
   else
       return -1;

在用户点击是或否按钮之前执行,我该怎么办?

5 个答案:

答案 0 :(得分:2)

据我了解,您的代码与

类似
// some code
int result = Func();
DoSmthWithResult(result);
// some code

您的Func正在UI线程上运行,因此在创建对话框后,它继续执行并返回mWich的初始值。您应该从DoSmthWithResult致电onClickListener而不是:

private DialogInterface.OnClickListener mListener = new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        DoSmthWithresult(which == -1 ? 0 : -1);
    }
};

现在DoSmthWithResult将在用户按下“是”或“否”按钮后执行。

答案 1 :(得分:0)

我认为你的方式不可能,因为Func()可能会在用户点击任一按钮之前完成执行。完成执行后,无法更改方法的返回值。

由于您已使用which存储mWhich.setWhich(which);。您可以稍后使用其他函数读取该值。

private DialogInterface.OnClickListener mListener = new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        mWhich.setWhich(which);
    }
};

public void showDialog() {
    final AlertDialog d = new AlertDialog.Builder(mContext).setTitle("Warning")
            .setCancelable(false).setMessage(alert).setPositiveButton("Yes", mListener)
            .setNegativeButton("No", mListener).create();
    d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    d.show();
}

public int func() {
    if (mWhich.getWhich() == -1)
        // the "yes" button was clicked
        return 0;
    else
        // the "no" button was clicked
        return -1;
}

答案 2 :(得分:0)

虽然它肯定是使用侦听器并在点击时启动新的调用层次结构的android方式(如Yuriy和Binoy所提议的),但是可能需要获得阻止对话框。我意识到通过启动一个带有自己的looper的新线程来显示对话框。这样ui线程可以自由运行,但不会产生ANR。

答案 3 :(得分:0)

我需要相同的功能来显示确认对话框,这是我的解决方法:

您的Func()就像我的messageDialog()一样:

    public static void messageDialog( Context context, String title, String bt_ok, String bt_cancel, final Runnable if_ok, final Runnable if_cancel ) {

        AlertDialog.Builder alert = new AlertDialog.Builder(context);

        alert.setTitle( title );

        alert.setPositiveButton( bt_ok.toUpperCase(),
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        if(if_ok!=null) if_ok.run();

                    }
                });
        alert.setNegativeButton( bt_cancel.toUpperCase(),
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        if(if_cancel!=null) if_cancel.run();

                    }
                });

        alert.show();

    }

以这种方式致电messageDialog()

    Runnable ifTrue = new Runnable() {
        @Override
        public void run() {
            //put here your YES onClick stuff
        }
    };

    String msg = "Do you really want to do this?";

    ALITEC.Dialogs.messageDialog( this, msg, "Yes", "No", ifTrue, null);

点击否按钮,我什么都不做......但是如果你想要的话:

    Runnable ifFalse = new Runnable() {
        @Override
        public void run() {
            //put here your NO onClick stuff
        }
    };

然后致电

  

messageDialog(this,msg," Yes"," No",ifTrue,ifFalse);

而不是

  

messageDialog(this,msg," Yes"," No",ifTrue,null);

这就是全部!

祝你好运!

答案 4 :(得分:0)

添加处理接收到的消息的Handler

 public static boolean areYouSure(Context context, String message){

    final boolean[] result = new boolean[1];
    final Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message mesg)
        {
            throw new RuntimeException();
        }
    };

    AlertDialog.Builder alert = new AlertDialog.Builder(context);
    alert.setTitle(message);

    alert.setPositiveButton("OK", (dialog, whichButton) -> {
        //What ever you want to do with the value
        result[0] = true;
        handler.sendMessage(handler.obtainMessage());
    });

    alert.setNegativeButton("CANCEL", (dialog, whichButton) -> {
        // what ever you want to do with No option.
        result[0] = false;
        handler.sendMessage(handler.obtainMessage());
    });

    alert.show();

    try{ Looper.loop(); }
    catch(RuntimeException e){}

    return result[0];
}