add View by using WindowManager, but can back key press

送分小仙女□ 提交于 2019-11-30 15:43:40

Have your View override

public boolean dispatchKeyEvent(KeyEvent event) 

to do something when back is pressed.

Override dispatchKeyEvent of your View

    @Override
    public boolean dispatchKeyEvent(KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
{
    // handle back press
    // if (event.getAction() == KeyEvent.ACTION_DOWN)
    return true;
}
return super.dispatchKeyEvent(event);
}
lockup

You can do like this:

Add a Float window by WindowManager, for example, add a view to screen bottom:

WindowManager windowManager = (WindowManager) getActivity().getApplicationContext().getSystemService(Context.WINDOW_SERVICE);

WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(); 
layoutParams.width   = WindowManager.LayoutParams.WRAP_CONTENT;
layoutParams.height  = WindowManager.LayoutParams.WRAP_CONTENT;
layoutParams.type= WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
layoutParams.flags   =  WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
layoutParams.format  = PixelFormat.TRANSLUCENT;
layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
layoutParams.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;

FrameLayout view = new FrameLayout(getActivity()) {
            @Override
            public boolean dispatchKeyEvent(KeyEvent event) {
                if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                    // do you code
                    return true;
                }
                return super.dispatchKeyEvent(event);
            }
        };
windowManager.addView(view, layoutParams); 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!