5

我编写了一个返回值类型为 int 的函数,在这个函数中我需要弹出一个带有两个按钮的 AlertDialog。当点击“Yes”按钮时,函数返回0,“No”按钮返回-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;

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

4

5 回答 5

2

据我了解,您的代码看起来类似于

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

Func正在 UI 线程上运行,因此在您创建对话框后,它会继续执行并返回初始值mWich. 您应该改为调用DoSmthWithResultonClickListener

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

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

于 2012-12-03T09:16:22.917 回答
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);

在 NO 按钮单击时,我什么也没做……但是如果您愿意:

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

就这样!

此致!

于 2014-07-15T18:16:01.580 回答
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;
}
于 2012-12-03T09:11:28.000 回答
0

虽然它肯定是使用侦听器并在单击时启动新的调用层次结构的 android 方式(正如 Yuriy 和 Binoy 建议的那样),但可能需要获取阻塞对话框。我意识到通过使用自己的循环器启动一个新线程来显示对话框。这样 ui 线程可以自由运行,但不会导致 ANR。

于 2012-12-03T09:45:09.910 回答
0

添加处理接收到的消息的处理程序

 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];
}
于 2021-02-08T18:32:40.347 回答