我正在尝试在eclipse中为Android制作应用程序。我在其中使用webview的不同片段。但我想在加载webview时显示进度条,但我无法理解如何做到这一点我检查了许多教程但未能这样做。在这里让我最活跃的代码。
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.app.Fragment;
import android.annotation.SuppressLint;
//import android.graphics.Bitmap;
//import android.widget.ProgressBar;
//import android.app.ProgressDialog;
//import android.app.Activity;
@SuppressLint("SetJavaScriptEnabled")
public class AdminTab1 extends Fragment {
private WebView webView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
View rootView = inflater.inflate(R.layout.admintab1, container, false);
webView = (WebView) rootView.findViewById(R.id.webView1);
webView.setWebViewClient(new WebViewClient());
webView.setScrollbarFadingEnabled(false);
webView.setHorizontalScrollBarEnabled(false);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setUserAgentString("AndroidWebView");
webView.clearCache(true);
webView.loadUrl("http://ww.google.com");
return rootView;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.mainmenu, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
webView.reload();
break;
case R.id.item2:
webView.goBack();
break;
default:
break;
}
return true;
}
}
答案 0 :(得分:3)
你应该试试这个
public class About27Fragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.about_27, container, false);
final WebView webView = (WebView) rootView.findViewById(R.id.webView_about27);
final ProgressDialog dialog = ProgressDialog.show(getActivity(), "", "Please wait, Loading Page...", true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
dialog.show();
}
@Override
public void onPageFinished(WebView view, String url){
dialog.dismiss();
String webUrl = webView.getUrl();
}
});
webView.loadUrl("https://www.google.com.mx");
return rootView;
}
}
答案 1 :(得分:1)
WebViewClient
有很多回调。例如,您应该对其进行子类化并覆盖onPageStarted
以显示并onPageFinished
将其关闭。查看文档here
private class MyWebClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url,
// here show the dialog
}
public void onPageFinished(WebView view, String url) {
// here dismiss it
}
}
并在onCreateView中使用它
webView.setWebViewClient(new MyWebClient());
答案 2 :(得分:1)
<强> OnCreate中强>
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.web_view);
web_view = (WebView) findViewById(R.id.web_view);
pd = new ProgressDialog(Activity.this);
pd.setMessage("Please wait Loading...");
pd.show();
web_view.setWebViewClient(new MyWebViewClient());
web_view.loadUrl("ur site name");
}
<强> WebViewClient 强>
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
if (!pd.isShowing()) {
pd.show();
}
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
System.out.println("on finish");
if (pd.isShowing()) {
pd.dismiss();
}
}
通过这种方式,您可以在webview中实现进度对话框。
答案 3 :(得分:0)
尝试此操作以获得百分比
的进展myWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different
// scales
//Updaate progress bar here with progress int
}
});