如何在android中向webview加载url时显示进度?
我正在将url加载到webview中:如何在android中向webview加载url时显示进度?,android,webview,Android,Webview,我正在将url加载到webview中: WebView webview=(WebView)findViewById(R.id.webview); webview.loadUrl(url); 加载url需要一些时间,在此期间,它会显示一个空白屏幕。我想在加载url时显示进度对话框: ProgressDialog dialog = ProgressDialog.show(this, "HI","Loading......", true); 但是,上述代码不起作用。如果有任何想法,请帮助。提前谢
WebView webview=(WebView)findViewById(R.id.webview);
webview.loadUrl(url);
加载url需要一些时间,在此期间,它会显示一个空白屏幕。我想在加载url时显示进度对话框:
ProgressDialog dialog = ProgressDialog.show(this, "HI","Loading......", true);
但是,上述代码不起作用。如果有任何想法,请帮助。提前谢谢 您需要通过扩展类为您的WebView设置自己的WebViewClient 您需要实现onPageStarted(此处显示)和onPageFinished(此处取消)两种方法
有关此主题的更多指导可以在Google的中找到。将
WebViewClient
设置为您的WebView,在onCreate()
方法上启动进度对话框,并在页面完成加载后在onPageFinished(WebView视图,字符串url)
您的main.xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView android:id="@string/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
查看示例代码。这对你有帮助
private ProgressBar progressBar;
progressBar=(ProgressBar)findViewById(R.id.webloadProgressBar);
WebView urlWebView= new WebView(Context);
urlWebView.setWebViewClient(new AppWebViewClients(progressBar));
urlWebView.getSettings().setJavaScriptEnabled(true);
urlWebView.loadUrl(detailView.getUrl());
public class AppWebViewClients extends WebViewClient {
private ProgressBar progressBar;
public AppWebViewClients(ProgressBar progressBar) {
this.progressBar=progressBar;
progressBar.setVisibility(View.VISIBLE);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
}
谢谢。您将不得不使用onPageStarted和onPageFinished回调
mWebView.setWebViewClient(new WebViewClient() {
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if (progressBar!= null && progressBar.isShowing()) {
progressBar.dismiss();
}
progressBar = ProgressDialog.show(WebViewActivity.this, "Application Name", "Loading...");
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
alertDialog.setTitle("Error");
alertDialog.setMessage(description);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alertDialog.show();
}
});
将ProgressBar的可见性设置为GONE是错误的-视图必须被删除才能从视图堆栈中删除。感谢这个好例子,你救了我一天!可能重复:
mWebView.setWebViewClient(new WebViewClient() {
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if (progressBar!= null && progressBar.isShowing()) {
progressBar.dismiss();
}
progressBar = ProgressDialog.show(WebViewActivity.this, "Application Name", "Loading...");
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
alertDialog.setTitle("Error");
alertDialog.setMessage(description);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alertDialog.show();
}
});
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
alertDialog.setTitle("Error");
alertDialog.setMessage(description);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alertDialog.show();
}
});