如何在webview中添加进度条线

时间:2015-04-04 09:14:15

标签: java android webview progress-bar

我正在尝试向使用webview的应用程序添加进度条或加载栏。我必须设置应用程序来运行应用程序中的所有链接,但我对如何在点击链接时实现进度条感到困惑

这是我的代码 公共类MainActivity扩展ActionBarActivity {

private WebView mWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mWebView = (WebView) findViewById(R.id.activity_main_webview);
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mWebView.loadUrl("http://caknowledge.in");
    mWebView.setWebViewClient(new MyAppWebViewClient());
}

@Override
public void onBackPressed() {
    if(mWebView.canGoBack()) {
        mWebView.goBack();
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

private class MyAppWebViewClient extends WebViewClient {
}

XML.JAVA代码如下

<WebView android:id="@+id/activity_main_webview" android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

3 个答案:

答案 0 :(得分:0)

只需在webview上方使用progressBar,当您的网页视图开始加载页面时,可以看到它,并且当您的webview页面加载时隐藏进度条

答案 1 :(得分:0)

您可以在webview客户端上添加以下代码以显示进度对话框

private class MyAppWebViewClient extends WebViewClient {
    @Override
    public void onPageStarted(WebView p_view, String p_url, Bitmap p_favIcon)
    {
        try {
            if (progressDialog != null && !isFinishing())
                progressDialog.show();
        } catch (Throwable ex) {
            //ignore
        }
        super.onPageStarted(p_view, p_url, p_favIcon);
    }
    @Override
    public void onPageFinished(WebView p_view, String p_url)
    {
        try {
            if (progressDialog != null && progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
        } catch (Throwable ex) {
            //ignore
        }
        super.onPageFinished(p_view, p_url);
    }
}

活动中的进度条声明

progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading..");

希望它适合你!!

答案 2 :(得分:0)

布局代码

  <ProgressBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/progress_bar"
     style="?android:attr/progressBarStyleHorizontal"/>

<WebView
    android:layout_below="@id/progress_bar"
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

java代码

webView.setWebViewClient(new HelloWebViewClient()); 
progressBar.setVisibility(View.INVISIBLE);


private class HelloWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon)
    {
        // TODO show you progress image

        progressBar.setVisibility(View.VISIBLE);

        super.onPageStarted(view, url, favicon);
    }

    @Override
    public void onPageFinished(WebView view, String url)
    {    progressBar.setVisibility(View.INVISIBLE);
        // TODO hide your progress image
        super.onPageFinished(view, url);
    }

}