首页 > 解决方案 > 在触摸外部时关闭弹出窗口

问题描述

我正在尝试打开一个带有 4 个按钮的弹出窗口,当按下按钮或用户在弹出窗口之外单击时,该窗口将关闭。我只会发出警告对话,但这只会支持 3 个按钮。

关于同一件事有很多问题,我找不到任何一致的答案或任何适合我的答案(包括已弃用的 Bitmap Drawable)。我已将我看到的所有建议都放入我的代码中,但无济于事。

这是我到目前为止使用的所有内容:

//to create new popup window
LayoutInflater chooseMealInflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View chooseMealLayout = chooseMealInflater.inflate(R.layout.choose_meal_dialog, null);

PopupWindow chooseMealPopup = new PopupWindow(chooseMealLayout, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true);


//to make popup dismiss on touch outside
chooseMealPopup.setOutsideTouchable(true);
chooseMealPopup.setFocusable(true);
chooseMealPopup.setContentView(chooseMealLayout);
chooseMealPopup.showAtLocation (chooseMealLayout, Gravity.CENTER,0,0);
chooseMealPopup.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

我试图找到我能找到的所有东西,比如保留setFocusablebefore showAtLocation,但是当我运行应用程序时,单击时没有任何反应。认为这可能与我的代码有关,因为我是新手,并不真正知道自己在做什么。

标签: javaandroid-studiopopuppopupwindow

解决方案


不知道你真正想做什么...

如果您想在单击按钮时显示对话框

YourBtn.setOnClickListener(new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            //what you want to do like show dialog

        }
    });

推荐阅读