我已经使用进度对话框创建了简单的webview,我的问题是当url被重定向的进度对话框不会被忽略时。例如:我打开网址:www.myweb.com/account。然后它重定向到:www.myweb.com/login。任何处理这个的解决方案?
//progress dialog
ProgressDialog loading;
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
loading= new ProgressDialog(MainActivity.this, R.style.MyAlertDialogStyle);
loading.setMessage("Sedang Memuat...");
loading.setCancelable(false);
loading.show();
}
@Override
public void onPageFinished(WebView view, String url) {
if (loading.isShowing()) {
loading.dismiss();
}
super.onPageFinished(view, url);
}
答案 0 :(得分:0)
webView.setWebViewClient(new WebViewClientClass());
String url = "http://google.com/";
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webView.loadUrl(url);
private class WebViewClientClass extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
prDialog = new ProgressDialog(NewsActivity.this);
prDialog.setMessage("Please wait ...");
prDialog.show();
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if(prDialog!=null){
prDialog.dismiss();
}
}
}
答案 1 :(得分:0)
loading.setCancelable(false);
将此设置为true为:
loading.setCancelable(true);
而不是这个:
if (loading.isShowing()) {
loading.dismiss();
}
super.onPageFinished(view, url);
使用此:
super.onPageFinished(view, url);
if (loading!=null) {
loading.dismiss();
}
同时尝试评论此行:
super.onPageFinished(view, url);
另一种方式是,如果它不以某种方式解雇你可以隐藏这种情况你必须这样做:
后:
loading.show();
添加:
loading.setVisibility(View.VISIBLE);
之后:
loading.dismiss();
添加:
loading.setVisibility(View.INVISIBLE);
答案 2 :(得分:0)
我不认为每个重定向都会调用onPageFinished
,这可能是由于使用了Iframe
或其他内容。我认为您应该在onProgressChanged
中覆盖WebChromeClient
,如下所示:
myWebView.setWebChromeClient(new WebChromeClient(){
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
if(newProgress <100){
prDialog.setMessage("Please wait ...");
prDialog.show();
}
else if (newProgress ==100){
if(prDialog!=null && prDialog.isShowing()){
prDialog.dismiss();
}
}
}