簡體   English   中英

如何在WebView中添加進度欄行

[英]How to add progress bar line in webview

我正在嘗試向使用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" />

只需使用webview上方的progressBar,然后在web view開始加載頁面時使其可見,並在webview頁面加載時隱藏progressbar

您可以在webviewclient上添加以下代碼以顯示進度對話框

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..");

希望這對你有用 !!

布局代碼

  <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);
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM