【发布时间】:2012-12-30 17:23:34
【问题描述】:
如何在加载网页时获取资源 url onProgressChanged(WebView view, int progress) 回调?我在这里需要它 因为从缓存加载时,onloadresource 不会在已缓存的资源上调用。谢谢
【问题讨论】:
标签: android
如何在加载网页时获取资源 url onProgressChanged(WebView view, int progress) 回调?我在这里需要它 因为从缓存加载时,onloadresource 不会在已缓存的资源上调用。谢谢
【问题讨论】:
标签: android
这段代码 sn-p 可能有助于显示中间进度:
Button btnLoad = (Button) findViewById(R.id.btn_load);
btnLoad.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
EditText etUrl = (EditText) findViewById(R.id.et_url);
mWvUrl.loadUrl(etUrl.getText().toString());
activity.setProgressBarIndeterminateVisibility(true);
}
});
mWvUrl.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
/** This prevents the loading of pages in system browser */
return false;
}
/** Callback method, executed when the page is completely loaded */
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Toast.makeText(getBaseContext(),
"Page loaded Completely",
Toast.LENGTH_SHORT).show();
/** Hiding Indeterminate Progress Bar in the title bar*/
activity.setProgressBarIndeterminateVisibility(false);
}
});
【讨论】:
当您使用 WebChromeClient 时,您可以根据您的目的覆盖 onProgressChanged(WebView view, int progress) 这个方法,这样的事情就可以完成。
yourWebViewObject.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
currentActivity.setTitle("Loading...");
currentActivity.setProgress(progress * 100);
if(progress == 100)
currentActivity.setTitle(R.string.app_name);
}
});
对于示例程序,您可以使用Using Android’s WebView, WebChromeClient and WebViewClient to load a webpage and display the progress。
【讨论】:
试试这个代码:
final ProgressBar Pbar;
final TextView txtview = (TextView)findViewById(R.id.tV1);
Pbar = (ProgressBar) findViewById(R.id.pB1);
MyView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
if(progress < 100 && Pbar.getVisibility() == ProgressBar.GONE){
Pbar.setVisibility(ProgressBar.VISIBLE);
txtview.setVisibility(View.VISIBLE);
}
Pbar.setProgress(progress);
if(progress == 100) {
Pbar.setVisibility(ProgressBar.GONE);
txtview.setVisibility(View.GONE);
}
}
});
【讨论】: