Android 如何清除 WebView 内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3715482/
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-08-20 11:30:17  来源:igfitidea点击:

How to clear WebView content?

android

提问by peterlawn

I have a WebViewin my app with wrap_contentwidth and height

WebView我的应用程序中有一个wrap_content宽度和高度

Before I use webview.loadData, the width and height of the it was 0, after I load a page (or I use webview.loadData) , it'll display a web page.

在我使用之前webview.loadData,它的宽度和高度是 0,在我加载一个页面(或者我使用webview.loadData)之后,它会显示一个网页。

My question is , how can I clear the webview to recover it back to the original state, just as before it loadData?

我的问题是,如何清除 webview 以将其恢复到原始状态,就像以前一样loadData

回答by Grantland Chew

I found this answer, pretty helpful: How do I clear a WebView's content before loading a page?

我发现这个答案非常有帮助:如何在加载页面之前清除 WebView 的内容?

It is for Cocoa, but basically just call:

它适用于可可,但基本上只是调用:

webView.loadUrl("about:blank")

回答by sandalone

The only complete solution is to check for SDK version.

唯一完整的解决方案是检查 SDK 版本。

if (Build.VERSION.SDK_INT < 18) {
   webView.clearView();
} else {
   webView.loadUrl("about:blank");
}

回答by Kuldip Bairagi

use following statement before loadData:

在 loadData 之前使用以下语句:

view.loadUrl("javascript:document.open();document.close();");

回答by Sudheesh VS

WebView.clearView();

WebView.clearView();

This will clear the view from the webview.

这将从 webview 中清除视图。

回答by Adam

I used the clearView() method, however I'm now having issues that when I loadData() right after it, it doesn't seem to load the data.

我使用了 clearView() 方法,但是我现在遇到了问题,当我在它之后 loadData() 时,它似乎没有加载数据。

回答by HelloWorld

What about loadUrl("about:blank"), and then clearHistory()?

loadUrl("about:blank") 和 clearHistory() 呢?