Android webview页面返回键

设置webview支持js页面

settings.setJavaScriptEnabled(true);
//设置监听,拦截url让接口跳转只在本app内部跳转,不跳转浏览器
 web_h5.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url != "") {
                    view.loadUrl(url);
                }
                //true,拦截url在本app内进行页面跳;false,不拦截,会跳到别的浏览器
                return true;
            }
        });


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && web_h5.canGoBack()) {
            web_h5.goBack();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

 /**
         *
         * 防止遇到重定向
         */
        web_h5.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (keyCode == KeyEvent.KEYCODE_BACK && web_h5.canGoBack()) {
                        web_h5.goBack();
                        return true;
                    }
                }

                return false;
            }
        });

你可能感兴趣的:(webview)