android 如何监听window的back键

来源:互联网 发布:下腹部赘肉怎么减 知乎 编辑:程序博客网 时间:2024/06/06 06:46

如何监听window的back键?


可以重写window的根view的dispatchKeyEvent方法:

WindowManager mWindowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);Display display = mWindowManager.getDefaultDisplay();int width = display.getWidth();int height = display.getHeight();int statusBarHeight = getStatusBarHeight();final PopupWindow pw = new PopupWindow(width, height - statusBarHeight);final FrameLayout container = new FrameLayout(getContext()){@Overridepublic boolean dispatchKeyEvent(KeyEvent event) {if(event.getAction() == KeyEvent.ACTION_UP) {if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {Log.addLog(this, "window height =" + pw.getHeight());removeAllViews();pw.dismiss();return true;}}return super.dispatchKeyEvent(event);}};container.addView(view, params);pw.setContentView(container);pw.setFocusable(true);pw.setAnimationStyle(R.style.popUpWindowAnimation);pw.showAtLocation(this, Gravity.NO_GRAVITY, 0, statusBarHeight);

windowManager 也一样。

0 0
原创粉丝点击