Android-防止在PopupWindow外部单击按钮

jmc*_*kie 3 android popupwindow

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

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);
                }
            });
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

Rah*_*rma 5

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

popupRAMS.setFocusable(true);
Run Code Online (Sandbox Code Playgroud)

与

popupRAMS.setFocusable(false);
Run Code Online (Sandbox Code Playgroud)

也尝试添加

popupRAMS.setOutsideTouchable(false);
Run Code Online (Sandbox Code Playgroud)

希望它能对您有所帮助。

  • 这在一定程度上有效,因为它不允许在外部单击时关闭弹出窗口,但是我还需要防止单击下面弹出窗口位于顶部的视图中的所有内容。 (2认同)