【问题标题】:Dismiss Popup Window on Touch Outside在触摸外部时关闭弹出窗口
【发布时间】:2019-07-09 18:45:52
【问题描述】:

我正在尝试打开一个带有 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));

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

【问题讨论】:

    标签: java android-studio popup popupwindow


    【解决方案1】:

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

    如果你想在点击按钮时显示对话框

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-21
      • 1970-01-01
      相关资源
      最近更新 更多