如何在弹出窗口中使用对话框?

时间:2017-07-24 15:03:23

标签: java android

我有一个弹出窗口,当按下按钮时会执行该窗口。当用户点击" home"在弹出窗口中,我想要弹出一个对话消息,询问用户是否确定要退出。

以下是我如何声明弹出窗口:

            LayoutInflater inflater = (LayoutInflater) endlessflags.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            layout = inflater.inflate(R.layout.success_menu, (ViewGroup) findViewById(R.id.myPop5));

            pwindo4 = new PopupWindow(layout, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
            pwindo4.setAnimationStyle(R.style.AnimationPopup);
            pwindo4.showAtLocation(layout, Gravity.TOP | Gravity.CENTER, 0, 0);

然后我在按钮上有一个onTouchListener,它也在弹出窗口中调用。我尝试调用我创建的对话功能,但是当我什么也没发生时,我没有错误或崩溃:

        AlertDialog.Builder myAlert = new AlertDialog.Builder(mainActivity.this);
        myAlert.setMessage("Would you like to quit?").setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            saveState = true;
            saveState();
            //finish();
            dialog.cancel();
        }
        }).setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            saveState = false;
            dialog.cancel();
            }
        });

我的问题是,为什么当我调用对话事件功能时似乎没有任何事情发生,我怎么能解决这个问题,以便在我点击弹出窗口中的按钮时显示对话框?

1 个答案:

答案 0 :(得分:1)

您需要显示对话框:

AlertDialog dialog = myAlert.create();
dialog.show();
相关问题