带有脚本onPageStarted和onPageFinished

时间:2017-10-29 16:01:05

标签: android webview android-progressbar

如何使用onPageStarted和onPageFinished在WebView中显示基于 MainActivity 进度条?我目前使用Android Studio 2.3.3

MainActivity



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

        WebView wv = (WebView) findViewById(R.id.webview1);
        wv.loadUrl("https://www.google.com);
        wv.setWebViewClient(new WebViewClient());

        WebSettings ws = wv.getSettings();
        ws.setJavaScriptEnabled(true);
        ws.setSupportZoom(false);

               }




2 个答案:

答案 0 :(得分:0)

试试这个:

ProgressBar progressBar;

...

progressBar = (ProgressBar) findViewById(R.id.progressbar);
progressBar.setMax(100);
progressBar.setProgress(1);

webView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
            progressBar.setProgress(progress);
        }
    });
        webView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            progressBar.setVisibility(View.VISIBLE);


        }



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

        @Override
        public void onPageFinished(WebView view, String url) {

            progressBar.setVisibility(View.GONE);
        }
    });

答案 1 :(得分:0)

<强> MainActivity

public class MainActivity extends AppCompatActivity {

WebView wv;
ProgressBar progressBar;

@Override
public void onBackPressed() {
    wv = (WebView) findViewById(R.id.webview1);
    if (wv.canGoBack()) {
        wv.goBack();
    } else {
        new AlertDialog.Builder(this)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setTitle("Sair")
                .setMessage("Você deseja sair do aplicativo?")
                .setPositiveButton("Sim", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }

                })
                .setNegativeButton("Não", null)
                .show();
    }
}


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

    wv = (WebView) findViewById(R.id.webview1);
    progressBar = (ProgressBar) findViewById(R.id.progressBar2);

    wv.setWebViewClient(new WebClient());
    wv.getSettings().setJavaScriptEnabled(true);
    wv.getSettings().setSupportZoom(false);
    wv.loadUrl("https://www.google.com");
}


public class WebClient extends WebViewClient {
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        progressBar.setVisibility(View.VISIBLE);
    }


    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        progressBar.setVisibility(View.GONE);
    }


}

}