0 android android-webview android-progressbar
我已经开发了一个具有一个Web视图和一个进度条的应用程序,所以我想在加载网页的进度条中添加显示进度等功能,以便它如何实现我有我放在这里的代码.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web = (WebView) findViewById(R.id.web);
progressBar = (ProgressBar) findViewById(R.id.progrss);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://www.technotalkative.com");
}
public class myWebClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
}
}
layout file..
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ProgressBar
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="center"
android:id="@+id/progrss"
style="?android:attr/progressBarStyleHorizontal"
android:progressDrawable="@drawable/blue_progressbar"
/>
<WebView
android:id="@+id/web"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1">
</WebView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
准备好你 progress bar喜欢
progressBar.setProgress(0);
probressbar.setMax(100);
Run Code Online (Sandbox Code Playgroud)
你应该intermediate progress使用
progressBar.setProgress(int);
Run Code Online (Sandbox Code Playgroud)
并实现你WebViewClient喜欢这样:
web.setWebViewClient(new myWebClient());
//add this below line
web.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
if (progresss == 100) {
progressBar.setVisibility(View.GONE);
} else{
progressBar.setVisibility(View.VISIBLE);
}
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1391 次 |
| 最近记录: |