0
点赞
收藏
分享

微信扫一扫

Android 如何实现点击空白区域关闭页面

实现以下两步即可

private Rect rect = new Rect();

public void onResume() {
        super.onResume();
        MainHandlerManager.postDelayed(() -> {
            int [] location = new int[2];
            cl_container.getLocationInWindow(location); //cl_container 外层容器
            int height = cl_container.getLayoutParams().height;
            int width = cl_container.getLayoutParams().width;
            rect = new Rect(location[0],location[1],width+location[0],height+location[1]);
        },100);
//        cl_container.getDisplay().getRectSize(rect);
    }

private boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        if (event.getY() < rect.bottom && event.getY() > rect.top && event.getX() > rect.left && event.getX() < rect.right) {
            return false;
        } else {
            finish();
            return true;
        }
    }
    return false;
}

举报

相关推荐

0 条评论