如何使用webview添加进度条? (使用WebViewClient而不是WebChromeClient)

时间:2017-05-01 07:40:51

标签: android webview

我有一个简单的应用程序,带有一个编辑文本,5个按钮,一个进度条和一个WebView。我有另一个类名myClient,它扩展了WebViewClient,我使用这个类来设置我的webviewclient和加载url。问题是我想使用progressBar显示进度,几乎所有的例子都在stackoverflow和互联网上使用WebChromeClient的方法onProgressChanged,但在我的情况下我不使用WebChromeClient类,我使用的是WebViewClient类。在这个类中我有onPageStarted和onPageFinished方法,但我如何使用它们来显示进度。

记住进度条在我的MainActivity中,onPageStarted和onPageFinished方法在另一个类中。

这是xml。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="13"


tools:context="com.hbss.MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:weightSum="4"
    >


    <EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/etUrl"
        android:layout_weight="3"
        />
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="GO"
        android:id="@+id/btnGo"
        />


</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:weightSum="4"
    >

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Back"
        android:id="@+id/btnBack"
        />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Forward"
        android:id="@+id/btnForward"
        />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Clear"
        android:id="@+id/btnClear"
        />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Reload"
        android:id="@+id/btnReload"
        />


</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
>
<WebView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/wb"
    />


</LinearLayout>

<ProgressBar
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:id="@+id/pb"
    android:max="100"
    android:indeterminate="false"
    style="?android:attr/progressBarStyleHorizontal"
    />


</LinearLayout>

这是Java代码。

MainActivity.Java

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText etUrl;
Button btnGo, btnBack, btnForward, btnClear, btnReload;
WebView wb;
String url;
ProgressBar pb;
ourClient oc;

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

    pb = (ProgressBar) findViewById(R.id.pb);
    pb.setVisibility(View.INVISIBLE);


    wb = (WebView) findViewById(R.id.wb);
    wb.getSettings().setJavaScriptEnabled(true);
    wb.getSettings().setUseWideViewPort(true);
    wb.getSettings().setLoadWithOverviewMode(true);
    wb.setWebViewClient(new ourClient());



    etUrl = (EditText) findViewById(R.id.etUrl);
    btnGo = (Button) findViewById(R.id.btnGo);
    btnBack = (Button) findViewById(R.id.btnBack);
    btnForward = (Button) findViewById(R.id.btnForward);
    btnClear = (Button) findViewById(R.id.btnClear);
    btnReload = (Button) findViewById(R.id.btnReload);

    btnBack.setEnabled(false);
    btnForward.setEnabled(false);

    btnGo.setOnClickListener(this);
    btnReload.setOnClickListener(this);
    btnBack.setOnClickListener(this);
    btnForward.setOnClickListener(this);
    btnClear.setOnClickListener(this);

    etUrl.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View view, int i, KeyEvent keyEvent) {
            if (keyEvent.getAction()==KeyEvent.ACTION_DOWN && i==KeyEvent.KEYCODE_ENTER) {
                load();
                return true;
            }

            return false;
        }
    });
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btnGo:
            load();
            break;
        case R.id.btnBack:
            if (wb.canGoBack())
                wb.goBack();
            break;
        case R.id.btnForward:
            if (wb.canGoForward())
                wb.goForward();
            break;
        case R.id.btnReload:
            wb.reload();
            break;
        case R.id.btnClear:
            wb.clearHistory();
            break;
    }
}


public void load(){
    url = etUrl.getText().toString();
    if (!url.startsWith("http://")) {
        url = "http://" + url;
    }
    try {
        wb.loadUrl(url);
        wb.requestFocus();
        View view = this.getCurrentFocus();
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(),0);
        if (wb.canGoBack()){
            btnBack.setEnabled(true);
        }

    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), " " + e, Toast.LENGTH_LONG).show();
    }

}

}

ourClient.java

public class ourClient extends WebViewClient {

public boolean shouldOverrideUrlLoading(WebView v, String url){
    v.loadUrl(url);
    return true;
}

}

2 个答案:

答案 0 :(得分:1)

试试这样的打击..

ProgressDialog   progressDialog_webview = new ProgressDialog(YourActivity.this);

        wv1.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                progressDialog_webview.setMessage("Please Wait....");
                progressDialog_webview.setCancelable(false);
                progressDialog_webview.setCanceledOnTouchOutside(false);
                progressDialog_webview.show();

                if (progress == 100) {
                    progressDialog_webview.cancel();


                } else {
                    progressDialog_webview.show();

                }
            }
        });

试试这个......

答案 1 :(得分:0)

试试这段代码

createWebview.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            if (mProgressDialog == null)
                mProgressDialog = new ProgressDialog(getActivity());
            mProgressDialog.setCancelable(false);
            mProgressDialog.show();
        }



        // when finish loading page
        public void onPageFinished(WebView view, String url) {

            if (mProgressDialog != null) {
                mProgressDialog.dismiss();
                mProgressDialog = null;
            }
        }

    });