如何使用后退按钮在加载之前关闭进度条?

发布于 2024-10-16 09:38:03 字数 2300 浏览 3 评论 0原文

我正在使用网页视图的进度条。我需要在页面加载之前停止进度条。我按后退按钮,但在加载之间不工作。

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         requestWindowFeature(Window.FEATURE_NO_TITLE);
         setContentView(R.layout.webview);
        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        final AlertDialog alertDialog = new AlertDialog.Builder(this).create();

        progressBar =new ProgressDialog(this);
        progressBar.setCancelable(true);
        progressBar.show();


        mWebView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.i("TEST", "Processing webview url click...");
                view.loadUrl(url);
                return true;
            }

            public void onPageFinished(WebView view, String url) {
                Log.i("TEST", "Finished loading URL: " +url);
                if (progressBar.isShowing()) {
                    progressBar.dismiss();
                }
            }

            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Log.e("TEST", "Error: " + description);
                Toast.makeText(getApplicationContext(), "Oh no! " + description, Toast.LENGTH_SHORT).show();
                alertDialog.setTitle("Error");
                alertDialog.setMessage(description);
                alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        return;
                    }



                });
                alertDialog.show();
            }
        });
        mWebView.loadUrl("http://www.google.com");
}

对于后退按钮测试我用这个

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            Toast.makeText(getApplicationContext(),"hi", Toast.LENGTH_LONG).show();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

I am using the progress bar for the web view.I need to stop progress bar before the page load.I press the back button but not working in between loading.

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         requestWindowFeature(Window.FEATURE_NO_TITLE);
         setContentView(R.layout.webview);
        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        final AlertDialog alertDialog = new AlertDialog.Builder(this).create();

        progressBar =new ProgressDialog(this);
        progressBar.setCancelable(true);
        progressBar.show();


        mWebView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.i("TEST", "Processing webview url click...");
                view.loadUrl(url);
                return true;
            }

            public void onPageFinished(WebView view, String url) {
                Log.i("TEST", "Finished loading URL: " +url);
                if (progressBar.isShowing()) {
                    progressBar.dismiss();
                }
            }

            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Log.e("TEST", "Error: " + description);
                Toast.makeText(getApplicationContext(), "Oh no! " + description, Toast.LENGTH_SHORT).show();
                alertDialog.setTitle("Error");
                alertDialog.setMessage(description);
                alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        return;
                    }



                });
                alertDialog.show();
            }
        });
        mWebView.loadUrl("http://www.google.com");
}

For back button test I use this

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            Toast.makeText(getApplicationContext(),"hi", Toast.LENGTH_LONG).show();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

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

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

发布评论

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

评论(3

洛阳烟雨空心柳 2024-10-23 09:38:03

你好
请检查一下,

progressBar = new ProgressDialog(this);
        progressBar.setCancelable(true);
        progressBar.setOnCancelListener(new OnCancelListener() {

            public void onCancel(DialogInterface arg0) {
                 if(progressBar.isShowing())
                      progressBar.dismiss();
                 finish();
            }
        });
            progressBar.setMessage("Loading...");
        progressBar.show();

谢谢大家

Hi
Please check this out

progressBar = new ProgressDialog(this);
        progressBar.setCancelable(true);
        progressBar.setOnCancelListener(new OnCancelListener() {

            public void onCancel(DialogInterface arg0) {
                 if(progressBar.isShowing())
                      progressBar.dismiss();
                 finish();
            }
        });
            progressBar.setMessage("Loading...");
        progressBar.show();

Thanks to every one

來不及說愛妳 2024-10-23 09:38:03

如果我是你,我会使用 AsyncTask 加载 URL 的数据,并在中间显示 ProgressDialog。

  • 启动时,生成一个不同的线程,该线程获取 URL 的内容
  • 开始显示 ProgressDialog
  • 当您拥有线程返回的 URL 的数据时,将其加载到 webView 中
  • 关闭 ProgressDialog

对于在不同线程中运行的内容,请检查 AsyncTask 。

If I were you, I would use AsyncTask to load the data of the URL, and show the ProgressDialog in between.

  • On start, spawn off a different thread which fetches the content of the URL
  • Start displaying the ProgressDialog
  • When you have the data of the URL that the thread returned, load it in the webView
  • Dismiss the ProgressDialog

For running things in different thread, check AsyncTask out.

痴者 2024-10-23 09:38:03

使用 UIWorker Thread 使用多线程实现进度和视觉外观

Use Multithreading to implement progress and visual appearence using UIWorker Thread

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