android webview stay in app

2019-01-11 13:30发布

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");

2条回答
Juvenile、少年°
2楼-- · 2019-01-11 13:55

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());
查看更多
ら.Afraid
3楼-- · 2019-01-11 13:59

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

myWebView.setWebViewClient(new WebViewClient());

It works for me.

查看更多
登录 后发表回答