我正在尝试打开一个弹出窗口,上面有 4 个按钮,当按下按钮或用户在弹出窗口外部单击时,该窗口将关闭。我只想制作一个警报对话框,但这仅支持 3 个按钮。
关于同一件事有很多问题,我找不到任何一致的答案或任何适合我的答案(包括已弃用的位图可绘制)。我已将我看到的所有建议都放入我的代码中,但无济于事。
这是我迄今为止使用过的所有内容:
//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
之前,但是当我运行应用程序时,单击时没有任何反应。我认为这可能是我的代码的个别问题,因为我是新手,并不真正知道自己在做什么。
最佳答案
不知道自己到底想做什么...
如果你想在点击按钮时显示对话框
YourBtn.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
//what you want to do like show dialog
}
});
关于java - 关闭触摸外部的弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56958904/