java android webview留在应用程序中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13542982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:09:43  来源:igfitidea点击:

android webview stay in app

javaandroidwebviewandroid-widget

提问by Carla Dessi

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:

我的 android 应用程序中有一个 webview,但是当有人浏览网站时,它会在一个新窗口中打开,我希望它留在 webview 内..有没有办法轻松做到这一点?这是我的活动中的代码:

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

回答by Ahmad

You'll have to create a WebViewClient:

你必须创建一个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 WebViewlike this:

然后将其设置为您WebView这样的:

webview.setWebViewClient(new myWebViewClient());

回答by quent

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

或者你可以这样做,而无需创建新类:

myWebView.setWebViewClient(new WebViewClient());

It works for me.

这个对我有用。

回答by Athira Reddy

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.

如果要自定义,则应覆盖shouldOverrideUrlLoading (WebView view, String url). 但它在 API 24 中已弃用。您可以使用public boolean shouldOverrideUrlLoading (WebView view,WebResourceRequest request). 实际上他们都需要返回false。

Android WebView Example

Android WebView 示例