本文介绍了Android:将数据加载到WebView时如何显示进度栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在将数据加载到Webview时如何显示进度栏?我的代码:
How can I show the progress bar while loading data into my webview?My code :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_PROGRESS);
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
this.setProgressBarVisibility(true);
setContentView(R.layout.layout_article);
WebView webview = (WebView) findViewById(R.id.webView);
final Activity activity = this;
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
activity.setProgress(progress * 100);
}
});
webView.loadUrl("http://google.com");
}
推荐答案
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ProgressBar progress = (ProgressBar) findViewById(R.id.progress);
WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
return false;
}
@Override
public void onPageStarted(final WebView view, final String url, final Bitmap favicon) {
progress.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(final WebView view, final String url) {
progress.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
});
webView.loadUrl("http://google.com");
}
}
和 R.layout.activity_main
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_centerInParent="true"/>
</RelativeLayout>
这篇关于Android:将数据加载到WebView时如何显示进度栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!