Android-防止按钮在PopupWindow外部单击 [英] Android - Prevent button click outside of PopupWindow

查看:107
本文介绍了Android-防止按钮在PopupWindow外部单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了一段时间尝试使它生效,并在网上寻找类似的解决方案,但似乎没有一个可行.我只需要单击生成"按钮就可以关闭我的PopupWindow,而不必单击窗口外部.有人遇到过这个问题吗?

I've spent a while trying to get this to work, looked for similar solutions online, but none seem to work. I need my PopupWindow to only be dismissed on the click of the generate button, not by clicking outside the window. Anyone encountered this issue before?

private void LoadRAMSPopup() {
    mainLayout.getForeground().setAlpha(150);
    LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);

    final View ramsView = layoutInflater.inflate(R.layout.popup_rams, null);
    final PopupWindow popupRAMS = new PopupWindow(
            ramsView,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
    );

    if (Build.VERSION.SDK_INT >= 21) {
        popupRAMS.setElevation(5.0f);
    }

    findViewById(R.id.mainLayout).post(new Runnable() {
        @Override
        public void run() {
            popupRAMS.showAtLocation(findViewById(R.id.mainLayout), Gravity.CENTER, 0, 0);
            popupRAMS.setOutsideTouchable(false);
            popupRAMS.setFocusable(true);
            popupRAMS.update();

            Button btnGenerate = (Button) ramsView.findViewById(R.id.btnGenerate);
            btnGenerate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(getApplicationContext(), CreateRAMSActivity.class);
                    startActivity(intent);
                    popupRAMS.dismiss();
                    mainLayout.getForeground().setAlpha(0);
                }
            });
        }
    });
}

推荐答案

设置 popupRAMS.setFocusable(false).删除使弹出窗口消失所需的不必要的触摸.所以请更换

Setting up popupRAMS.setFocusable(false). removes unnecessary touch required to make popup window disappear. So please replace

popupRAMS.setFocusable(true);

popupRAMS.setFocusable(false);

也尝试添加

popupRAMS.setOutsideTouchable(false);

希望它会对您有所帮助.

Hope it will help you out.

这篇关于Android-防止按钮在PopupWindow外部单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆