java - 在触摸外部时关闭弹出窗口
问题描述
我正在尝试打开一个带有 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
before showAtLocation
,但是当我运行应用程序时,单击时没有任何反应。认为这可能与我的代码有关,因为我是新手,并不真正知道自己在做什么。
解决方案
不知道你真正想做什么...
如果您想在单击按钮时显示对话框
YourBtn.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
//what you want to do like show dialog
}
});
推荐阅读
- sorting - 使用 Google 表格中的单个公式从范围创建动态排序和过滤列表
- jquery - jquery tableExport 导出负值在开头有引号
- android - 如何通过相机单击图像并在颤动中设置为图像视图
- amazon-web-services - 从 AWS Cognito 解码 JWT 公开 poolId 和客户端应用程序密钥
- android - 将 Smart Lock 与 Firebase 身份验证电子邮件+密码帐户结合使用
- python - 如何正确处理石墨烯错误?
- python - Python请求,没有协议的get()?
- c++ - 如何通过opencv cv :: calibrateCamera通过索引返回访问值?
- c - 如何通过将用户的主题作为c语言中的另一个结构成员来分配结构成员中的部分
- c# - 我可以删除使用 Filestream 打开的文件的第一部分吗?