webview中的Toast不会在onProgressChanged上消失

时间:2013-11-07 14:51:03

标签: android webview android-webview android-toast

在我的网页浏览中,我希望在页面加载时创建一个祝酒词,当进度为100%时,我希望吐司消失..实际上不起作用。这是代码

webview.setWebChromeClient(new WebChromeClient(){

                public void onProgressChanged(WebView view, int progress) {
                    activity.setTitle("Loading...");
                    activity.setProgress(progress * 100);
                    Toast toast = Toast.makeText(MainActivity.this, "Loading...",Toast.LENGTH_LONG);
                    toast.show();
                    if(progress == 100)
                    setProgressBarIndeterminateVisibility(false);
                    activity.setTitle("My WebView");
                    toast.cancel();
                }
            });

通过这种方式吐司不会出现在网页上我加载页面...如果我删除toast.cancel();吐司出现但永远不会消失..我该如何解决?

1 个答案:

答案 0 :(得分:1)

尝试这样

public void onProgressChanged(WebView view, int progress) {
                        activity.setTitle("Loading...");
                        activity.setProgress(progress * 100);

                        if(progress == 100)
                           {
                        Toast toast = Toast.makeText(MainActivity.this, "Finish...",Toast.LENGTH_LONG);
                        toast.show();
                           }
                        setProgressBarIndeterminateVisibility(false);
                        activity.setTitle("My WebView");
                    }
                });

或者尝试这样,

this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
            setContentView(R.layout.info);

            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgressDialog.setMessage("Please wait...");
            mProgressDialog.setCancelable(false);
            mProgressDialog.setMax(100);
            mProgressDialog.show();

            WebView webView = (WebView) findViewById(R.id.webview);
            webView.setVisibility(View.VISIBLE);
            webView.getSettings().setJavaScriptEnabled(true);

            webView.setWebChromeClient(new WebChromeClient() {
                public void onProgressChanged(WebView view, final int progress)
                {
                    activity.setTitle("Loading...");
                    activity.setProgress(progress * 100);
                    mProgressDialog.setProgress(progress );




                    if(progress == 100)
                    {
                        activity.setTitle(R.string.app_name);
                        mProgressDialog.dismiss();
                    Toast toast = Toast.makeText(MainActivity.this, "Finish...",Toast.LENGTH_LONG);
                        toast.show();
                    }

                }
            });

            webView.setWebViewClient(new WebViewClient() {
                @Override
                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
                {
                }

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