[英]Loading Dialog Box for Webview
这件事使我发疯。 我的应用程序加载了webview,并在初始加载时显示了加载对话框。 我希望每次单击链接或每次加载Webview时都出现加载对话框。 这没有发生。
Eclipse告诉我onPageStarted()不在本地使用,尽管onPageFinished()可以正常工作!
任何人都可以看到发生了什么事情吗,我在下面粘贴了所有活动:
package com.jeh.myapp;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MyActivity extends Activity {
WebView myWebView;
public static final String TAG = "Main";
public ProgressDialog progressBar;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Remove title bar as we already have it in the web app
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Point to the content view defined in XML
setContentView(R.layout.main);
//Configure the webview setup in the xml layout
myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
//Yes, we want javascript, pls.
webSettings.setJavaScriptEnabled(true);
progressBar = ProgressDialog.show(MyActivity.this, "Diag Title", "Loading...");
//Make sure links in the webview is handled by the webview and not sent to a full browser
myWebView.setWebViewClient(new WebViewClient() {
//this bit causes problems, if I add @Override here it says to remove, where as the current code marks onPageStarted yellow and says it's not used locally!? - yet onPageFinsihed() below works fine?
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressBar.show();
}
public void onPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " +url);
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
});
//Load URL:
myWebView.loadUrl("http://www.google.com");
}
简单。 没有
void onPageStarted(WebView view, String url)
WebViewClient中的方法,只需使用:
@Override
void onPageStarted(WebView view, String url, Bitmap favicon)
这些错误很容易造成,也很难发现。 您应该在任何要覆盖方法的地方使用@Override关键字(在您的情况下可能会标记错误)。
尝试通过以下方式修改您的源:
//Make sure links in the webview is handled by the webview and not sent to a full browser
myWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressBar.show();
}
@Override
public void onPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " +url);
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.