我正在使用网页视图的进度条。我需要在页面加载之前停止进度条。我按下后退按钮但不在加载之间工作。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.webview);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
progressBar =new ProgressDialog(this);
progressBar.setCancelable(true);
progressBar.show();
mWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i("TEST", "Processing webview url click...");
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
Log.i("TEST", "Finished loading URL: " +url);
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.e("TEST", "Error: " + description);
Toast.makeText(getApplicationContext(), "Oh no! " + description, Toast.LENGTH_SHORT).show();
alertDialog.setTitle("Error");
alertDialog.setMessage(description);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alertDialog.show();
}
});
mWebView.loadUrl("http://www.google.com");
}
对于后退按钮测试我使用此
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
Toast.makeText(getApplicationContext(),"hi", Toast.LENGTH_LONG).show();
return true;
}
return super.onKeyDown(keyCode, event);
}
答案 0 :(得分:4)
您好 请查看
progressBar = new ProgressDialog(this);
progressBar.setCancelable(true);
progressBar.setOnCancelListener(new OnCancelListener() {
public void onCancel(DialogInterface arg0) {
if(progressBar.isShowing())
progressBar.dismiss();
finish();
}
});
progressBar.setMessage("Loading...");
progressBar.show();
感谢每一个
答案 1 :(得分:1)
如果我是你,我会使用AsyncTask加载URL的数据,并在其间显示ProgressDialog。
对于在不同线程中运行的东西,请检查AsyncTask out。
答案 2 :(得分:0)
使用多线程使用工作线程实现进度和视觉外观