我正在开发一个简单的Android应用程序,使用Android Level 8 api加载单个网站。我在Eclipse中用Java编写代码。当用户点击应用程序中正常工作的网页上的链接时,我已经能够显示ProgressDialog。
然而,问题是第一次加载应用程序并加载第一个网页时。网页需要一段时间,它只是显示一个空白的白色屏幕,而第一页正在加载并开始显示元素(使用Android模拟器进行编程)。我想在第一页加载期间显示相同的ProgressDialog指标,但我的尝试都没有成功。
以下是我的代码。
package com.TestWebView;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class TestWebView extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView engine = (WebView) findViewById(R.id.web_engine);
WebViewClient myWebClient = new WebViewClient()
{
ProgressDialog pd = null;
// Override page so it's load on my view only
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
/*view.loadUrl(url);
return true;*/
//start the progress dialog
pd = ProgressDialog.show(TestWebView.this, "", "Loading...");
if (url.contains("maps.google.com") == true)
{
// Load new URL Don't override URL Link
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
return true;
}
return false;
}
@Override
public void onPageFinished(WebView view, String url){
pdHandler p = new pdHandler();
p.sendEmptyMessage(0);
}
class pdHandler extends Handler {
@Override
public void handleMessage(Message msg) {
if(pd != null)
{
pd.dismiss();
pd = null;
}
}
}
};
engine.getSettings().setJavaScriptEnabled(true);
engine.getSettings().setSupportZoom(true);
engine.getSettings().setBuiltInZoomControls(true);
engine.setWebViewClient(myWebClient);
engine.loadUrl("http://www.amazon.com/gp/aw/h.html");
}
}
感谢任何指导。
谢谢。
答案 0 :(得分:2)
使用此代码:
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
myLoadingDialog.show();
super.onPageStarted(view, url, favicon);
}
答案 1 :(得分:1)
final ProgressDialog pd = ProgressDialog.show(this, "", "Loading!", true);
setContentView(R.layout.webview);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
pd.dismiss();
}
});
mWebView.loadUrl("file:///android_asset/index.html");
答案 2 :(得分:0)
尝试这样:
ProgressDialog pg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
engine.setWebViewClient(new MyWebViewClient());
}
public class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//WebView link Loader
if(pg==null)
{
pg=new ProgressDialog(NowShowingActivity.this,R.style.MyTheme);
pg.setMessage("Loading...");
pg.setCancelable(false);
pg.show();
}
webViewNowShowing.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
try {
if (pg.isShowing()) {
pg.dismiss();
pg = null;
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
public MyWebViewClient() {
super();
}
//initial WebPage loader
public void onPageStarted(WebView webView, String url, Bitmap favicon) {
if(pg==null&&pullToRefresh==false){
pg =new ProgressDialog(NowShowingActivity.this,R.style.MyTheme);
pg.setMessage("Loading..");
pg.setCancelable(false);
pg.show();
}
}
}