Android AlertDialog onDismiss获得用户选择

时间:2015-04-04 20:38:02

标签: android alertdialog

考虑以下AlertDialog:

pizzaOrderDialog = new AlertDialog.Builder(mContext);
    pizzaOrderDialog.setTitle("One last thing:");
    pizzaOrderDialog.setMessage("Would you like black or green olives?")
            .setCancelable(true)
            .setPositiveButton("Black", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    orderPizza("black");
                    Toast.makeText(mContext, "Ok! Thanks!", Toast.LENGTH_LONG).show();
                }
            })
            .setNegativeButton("Green", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    orderPizza("green");
                    Toast.makeText(mContext, "Ok! Thanks!", Toast.LENGTH_LONG).show();
                }
            });
    alertD = pizzaOrderDialog.create();

用户也可以选择通过触摸屏幕上的任意位置来关闭对话框,而不是单击“黑色”或“绿色”。我想要的是“抓住”这个案例并在onDismiss中获得这种情况的指示:

alertD.setOnDismissListener(new DialogInterface.OnDismissListener() {

        @Override
        public void onDismiss(DialogInterface dialogInterface) {
            //TODO tell if dialog result was dialogInterface.BUTTON_NEGATIVE
            // or maybe dialogInterface.BUTTON_POSITIVE ?
        }
    });

这里的最佳做法是什么?谢谢!

1 个答案:

答案 0 :(得分:1)

显然,正如@CommonsWare指出的那样,我需要在这里使用setOnCancelListener