Webview 中的进度对话框

发布于 2024-12-29 00:50:54 字数 661 浏览 0 评论 0原文

简单地说,每次在我的 webview 中单击新链接时,如何才能显示进度对话框。我尝试了很多教程和方法,但每个教程和方法都只在应用程序最初加载时显示一次对话框。

这是我的代码

 mWebView.setWebViewClient(new WebViewClient() {

             @Override
             public boolean shouldOverrideUrlLoading(WebView view, String url)
             {
                 view.loadUrl(url);
                 return true;
             }
            ProgressDialog dialog = ProgressDialog.show(myActivity.this, "", 
                            "Loading. Please wait...", true);
            @Override
            public void onPageFinished(WebView view, String url) {
             dialog.dismiss();
           }

Simply put how can I make a progress dialog show up every time a new link is clicked in my webview. I have tried many tutorials and methods but every one of them only shows the dialog once, when the app is initially loaded.

Here is my code

 mWebView.setWebViewClient(new WebViewClient() {

             @Override
             public boolean shouldOverrideUrlLoading(WebView view, String url)
             {
                 view.loadUrl(url);
                 return true;
             }
            ProgressDialog dialog = ProgressDialog.show(myActivity.this, "", 
                            "Loading. Please wait...", true);
            @Override
            public void onPageFinished(WebView view, String url) {
             dialog.dismiss();
           }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云裳 2025-01-05 00:50:55

尝试这个最简单的,它对我有用

public class HomeWebViewActivity extends AppCompatActivity{

    private String weburl;
    private WebView webview;
    private Toolbar toolbar;
    private ProgressDialog prDialog;



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


         toolbar = (Toolbar) findViewById(R.id.toolbar_homewebview);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);


        weburl="ADD YOUR URL HERE";

        webview = (WebView) findViewById(R.id.webviewinfo);
        webview.setWebViewClient(new WebVwClientcls());


        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        webview.loadUrl(weburl);


    }

    private class WebVwClientcls 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) {
            super.onPageStarted(view, url, favicon);
            prDialog = new ProgressDialog(HomeWebViewActivity.this);
            prDialog.setMessage("Please wait ...");
            prDialog.show();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if(prDialog!=null){
                prDialog.dismiss();
            }
        }
    }
}

Try this easiest one,it work for me

public class HomeWebViewActivity extends AppCompatActivity{

    private String weburl;
    private WebView webview;
    private Toolbar toolbar;
    private ProgressDialog prDialog;



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


         toolbar = (Toolbar) findViewById(R.id.toolbar_homewebview);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);


        weburl="ADD YOUR URL HERE";

        webview = (WebView) findViewById(R.id.webviewinfo);
        webview.setWebViewClient(new WebVwClientcls());


        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        webview.loadUrl(weburl);


    }

    private class WebVwClientcls 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) {
            super.onPageStarted(view, url, favicon);
            prDialog = new ProgressDialog(HomeWebViewActivity.this);
            prDialog.setMessage("Please wait ...");
            prDialog.show();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if(prDialog!=null){
                prDialog.dismiss();
            }
        }
    }
}
无名指的心愿 2025-01-05 00:50:54

看看这个:

wvCouponsAndOffers.setWebViewClient(new WebViewClient() {
        ProgressDialog progressDialog = new ProgressDialog(CouponsWebViewUI.this);

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            progressDialog.setMessage(StringConstants.PROGRESS_DIALOG_MSG);
            progressDialog.setCancelable(false);
            progressDialog.setOnKeyListener(new OnKeyListener(){
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode,
                        KeyEvent event) {
                    if(keyCode == KeyEvent.KEYCODE_SEARCH) {
                        return true;
                    }
                    else
                        return false;
                }});
            progressDialog.show();
        }
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            progressDialog.setMessage(StringConstants.PROGRESS_DIALOG_MSG);
            progressDialog.setCancelable(false);
            progressDialog.setOnKeyListener(new OnKeyListener(){
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode,
                        KeyEvent event) {
                    if(keyCode == KeyEvent.KEYCODE_SEARCH) {
                        return true;
                    }
                    else
                        return false;
                }});
            progressDialog.show();
            view.loadUrl(url);
            return true;
        }

        public void onPageFinished(WebView view, String url) {
            if (progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
        }

    });

check out this:

wvCouponsAndOffers.setWebViewClient(new WebViewClient() {
        ProgressDialog progressDialog = new ProgressDialog(CouponsWebViewUI.this);

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            progressDialog.setMessage(StringConstants.PROGRESS_DIALOG_MSG);
            progressDialog.setCancelable(false);
            progressDialog.setOnKeyListener(new OnKeyListener(){
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode,
                        KeyEvent event) {
                    if(keyCode == KeyEvent.KEYCODE_SEARCH) {
                        return true;
                    }
                    else
                        return false;
                }});
            progressDialog.show();
        }
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            progressDialog.setMessage(StringConstants.PROGRESS_DIALOG_MSG);
            progressDialog.setCancelable(false);
            progressDialog.setOnKeyListener(new OnKeyListener(){
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode,
                        KeyEvent event) {
                    if(keyCode == KeyEvent.KEYCODE_SEARCH) {
                        return true;
                    }
                    else
                        return false;
                }});
            progressDialog.show();
            view.loadUrl(url);
            return true;
        }

        public void onPageFinished(WebView view, String url) {
            if (progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
        }

    });
流年已逝 2025-01-05 00:50:54

试试这个,

 // Let's display the progress in the activity title bar, like the
 // browser app does.
 getWindow().requestFeature(Window.FEATURE_PROGRESS);

 webview.getSettings().setJavaScriptEnabled(true);

 final Activity activity = this;
 webview.setWebChromeClient(new WebChromeClient() {
   public void onProgressChanged(WebView view, int progress) {
     // Activities and WebViews measure progress with different scales.
     // The progress meter will automatically disappear when we reach 100%
     activity.setProgress(progress * 1000);
   }
 });
 webview.setWebViewClient(new WebViewClient() {
   public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
     Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
   }
 });

 webview.loadUrl("http://slashdot.org/");

如果你想做出自己的进度对话框,然后

 webView.setWebViewClient(new WebViewClient() {

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

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

        //HIDE PROGRESS DIALOG LOADING IT HAS FINISHED
      }

    });

让我知道会发生什么..

Try this,

 // Let's display the progress in the activity title bar, like the
 // browser app does.
 getWindow().requestFeature(Window.FEATURE_PROGRESS);

 webview.getSettings().setJavaScriptEnabled(true);

 final Activity activity = this;
 webview.setWebChromeClient(new WebChromeClient() {
   public void onProgressChanged(WebView view, int progress) {
     // Activities and WebViews measure progress with different scales.
     // The progress meter will automatically disappear when we reach 100%
     activity.setProgress(progress * 1000);
   }
 });
 webview.setWebViewClient(new WebViewClient() {
   public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
     Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
   }
 });

 webview.loadUrl("http://slashdot.org/");

And if you want to make your own progressDialog then

 webView.setWebViewClient(new WebViewClient() {

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

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

        //HIDE PROGRESS DIALOG LOADING IT HAS FINISHED
      }

    });

And let me know what happen..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文