禁用弹出后退按钮 windows

Disabling the back button for popup windows

根据教授如何创建弹出窗口的 YouTube 教程视频 (https://www.youtube.com/watch?v=wxqgtEewdfo) windows,我想知道如何使用 触摸事件[=22] 关闭弹出窗口=] 而不是后退按钮...

这是 MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    RelativeLayout relative = (RelativeLayout)findViewById(R.id.relativeTest);

    Button button = (Button)findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext().
                    getSystemService(LAYOUT_INFLATER_SERVICE);
            ViewGroup container = (ViewGroup) layoutInflater.inflate(R.layout.myLayout, null);

            PopupWindow popupWindow = new PopupWindow(container, AbsListView.LayoutParams.MATCH_PARENT,
                    AbsListView.LayoutParams.MATCH_PARENT, true);
            popupWindow.showAtLocation(relative, Gravity.CENTER, 0, 0);

            container.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    popupWindow.dismiss();

                    return true;
                }
            });
        }
    });
}

@Override
public void onBackPressed() {
    // I want the back button to be disabled for both MainActivity and the
    // popup window.
}

...我应该将 onBackPressed() 放在其他地方还是可能?

提前致谢。

将 onBackPressed 留空。删除 super.OnBackPressed 行

试试这个:-

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
if (Integer.parseInt(android.os.Build.VERSION.SDK) > 5
        && keyCode == KeyEvent.KEYCODE_BACK) {
    Log.d("CDA", "onKeyDown Called");
    onBackPressed();
    return true; 
}
return super.onKeyDown(keyCode, event);
}

好的,我现在想通了(归功于 Filip,YouTube 视频的上传者)...问题是我包含了 PopupWindow 的最后一个参数 focusable 并将其设置为 true,并且尽快我按如下方式摆脱了它:

popupWindow = new PopupWindow(container, AbsListView.LayoutParams.MATCH_PARENT,
                    AbsListView.LayoutParams.MATCH_PARENT);

...弹出窗口现在只能通过触摸事件关闭。