我正在尝试使用WebView
在我的应用程序中打开一个网页。当我打开网页时,它会显示空白屏幕一段时间,然后在我的应用程序内的浏览器中打开该页面。
有人建议我如何显示进度或摆脱加载webview时出现的空白屏幕?
我正在使用以下代码:
WebView mWebView = (WebView) findViewById(R.id.mywebview);
mWebView.getSettings().setJavaScriptEnabled(true);
// error handling
final Activity activity = this;
mWebView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
// error handling
mWebView.loadUrl(URL);
mWebView.setWebViewClient(new HelloWebViewClient());
答案 0 :(得分:43)
试试这个:
getWindow().requestFeature(Window.FEATURE_PROGRESS);
WebView mWebView = (WebView) findViewById(R.id.mywebview);
mWebView.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
mWebView.setWebChromeClient(new WebChromeClient(){
public void onProgressChanged(WebView view, int progress) {
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle("My title");
}
});
mWebView.loadUrl(URL);
答案 1 :(得分:19)
以下是我正在使用的代码:
内部WebViewClient:
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
findViewById(R.id.progress1).setVisibility(View.VISIBLE);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
findViewById(R.id.progress1).setVisibility(View.GONE);
}
});
这是XML:
<ProgressBar
android:id="@+id/progress1"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
希望这会有所帮助..
答案 2 :(得分:11)
你需要做这样的事情:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_PROGRESS);
this.setProgressBarVisibility(true);
}
然后
final Activity activity = this;
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
activity.setProgress(progress * 100);
}
});
<强>更新强>
我知道这有点太晚了,但我没有注意到:你不应该使用setWebViewClient()
两次。第二个呼叫取消第一个呼叫,因此您不会得到错误处理。
答案 3 :(得分:3)
1。在oncreate中调用AsyncTask 。
2。在asynctask中,只需进入进度对话框并显示进度对话框。
3。在webview客户端中,您只需再次显示进度对话框,点击在您的webview中打开的网站的任何链接,在完成加载链接后,我们覆盖方法 onPageFinished ,在此方法中我们解雇进度对话框。
<强> OnCreate中强>
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.web_view);
web_view = (WebView) findViewById(R.id.web_view);
OpenWebSiteInWebView opensite = new OpenWebSiteInWebView();
opensite.execute();
}
<强>的AsyncTask 强>
private class OpenWebSiteInWebView extends AsyncTask<String, Void, String> {
@SuppressWarnings("deprecation")
@SuppressLint("SetJavaScriptEnabled")
@Override
protected String doInBackground(String... params) {
web_view.setWebViewClient(new MyWebViewClient());
web_view.loadUrl("ur site name");
return null;
}
@Override
protected void onPostExecute(String result) {
}
@Override
protected void onPreExecute() {
pd = new ProgressDialog(SiteOpenInWebView.this);
pd.setMessage("Please wait Loading...");
pd.show();
}
}
<强> WebViewClient 强>
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
if (!pd.isShowing()) {
pd.show();
}
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
System.out.println("on finish");
if (pd.isShowing()) {
pd.dismiss();
}
}
}
答案 4 :(得分:0)
以下是我在 KOTLIN 中使用的代码:
private fun setupWebView() {
val webViewClient: WebViewClient = object: WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
view?.loadUrl(request?.url.toString())
return super.shouldOverrideUrlLoading(view, request)
}
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
showProgressDialog()
super.onPageStarted(view, url, favicon)
}
override fun onPageFinished(view: WebView?, url: String?) {
hideProgressDialog()
super.onPageFinished(view, url)
}
}
webView.webViewClient = webViewClient
webView.settings.javaScriptEnabled = true
webView.settings.defaultTextEncodingName = "utf-8"
}