将ProgressBar添加到Webview中 - Android

时间:2014-03-12 00:01:53

标签: android webview progress-bar

有类似的问题,但我是一个新手并且因为每个人都有不同的编码风格而感到困惑...我想要一个进度条显示在'Splash Screen'后面而不是白屏10秒钟每次点击一个链接......

我怎么会这样实现,因为我不知道?我没有使用webchromeclient或任何其他东西。还在学习。这是我的代码;

XML中的ProgressBar;

<ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="23dp"
    android:layout_marginTop="20dp"
    android:indeterminate="false"
    android:max="100"
    android:minHeight="50dp"
    android:minWidth="200dp"
    android:progress="1" />

我的Java代码;

public class SecondActivity extends Activity {

 private WebView myWebView;
 private ProgressBar progressBar;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.secondlayout);
    String url = "http://ythaber.com/";

    myWebView = (WebView) this.findViewById(R.id.webView1);
    progressBar = (ProgressBar) findViewById(R.id.progressBar1);

    myWebView.setWebViewClient(new WebViewClient());
    myWebView.getSettings().setJavaScriptEnabled(true);
    myWebView.loadUrl(url);
    myWebView.getSettings().setLoadsImagesAutomatically(true);

}

@Override
// Detect when the back button is pressed
public void onBackPressed() {
    if (myWebView.canGoBack())
        myWebView.goBack();
    else if (myWebView.canGoForward())
        myWebView.goForward();
    }
}

谢谢,DG

1 个答案:

答案 0 :(得分:2)

实施如下: 的的onCreate()

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

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.setWebViewClient(new WebViewClient());
web_view.getSettings().setJavaScriptEnabled(true);
web_view.getSettings().setLoadsImagesAutomatically(true);
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();
 }
}