android webview stay in app

邮差的信 提交于 2019-12-17 10:57:42

问题


I have a webview in my android app, but when someone navigates around the site, it opens in a new window, i want it to stay inside the webview.. is there a way to do this easily? Here is the code in my activity:

super.onCreate(savedInstanceState);
    setContentView(R.layout.shop);
    WebView webview;
    webview = (WebView) findViewById(R.id.webview);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.loadUrl("http://www.google.co.uk");

回答1:


You'll have to create a WebViewClient:

public class myWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

And then set it to your WebView like this:

webview.setWebViewClient(new myWebViewClient());



回答2:


Or you can just do that, without creating a new class:

myWebView.setWebViewClient(new WebViewClient());

It works for me.




回答3:


webview.setWebViewClient(new WebViewClient());

If you want to customize then you should override shouldOverrideUrlLoading (WebView view, String url). but it's deprecated in API 24. You can use public boolean shouldOverrideUrlLoading (WebView view,WebResourceRequest request). actually both of them needs to return false.

Android WebView Example



来源:https://stackoverflow.com/questions/13542982/android-webview-stay-in-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!