Android Webview 加载对话框没有被关闭

发布于 2024-09-08 20:03:33 字数 680 浏览 8 评论 0原文

我正在使用以下代码

class CustomWebViewClient extends WebViewClient {
    Context context;
    ProgressDialog pd = null;


    public CustomWebViewClient (Context c){
        context = c;
    }

    public void onPageFinished(WebView view, String url){
        pd.dismiss();
    }



    public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
        pd = ProgressDialog.show(context, "", "pageload. Please wait...", true);


        view.loadUrl(url);  

        return true;
    } 

}

当我单击 WebView 中的链接时,会出现对话框并且页面开始加载,但是当页面加载完成时,对话框仍然在屏幕上。显然代码很简单,但我无法弄清楚。另外,我想我应该补充一点,所单击的链接有一些重定向,但我不确定这是否与此处的原因有关。

我怎样才能正确地做到这一点?

I am using the following code

class CustomWebViewClient extends WebViewClient {
    Context context;
    ProgressDialog pd = null;


    public CustomWebViewClient (Context c){
        context = c;
    }

    public void onPageFinished(WebView view, String url){
        pd.dismiss();
    }



    public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
        pd = ProgressDialog.show(context, "", "pageload. Please wait...", true);


        view.loadUrl(url);  

        return true;
    } 

}

When I click a link in the WebView, the dialog appears and the page begins to load, however when the page is finished loading, the dialog is still on the screen. Obviously the code is simple enough, but I cant figure this out. Also, I guess I should add that the links being clicked have a few redirects, but I am not sure if that is related to the cause here.

How can I do this right?

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

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

发布评论

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

评论(4

暗喜 2024-09-15 20:03:34

您错过了 @Override 注释。

这是正确的代码:

class CustomWebViewClient extends WebViewClient {
    Context context;
    ProgressDialog pd = null;

    public CustomWebViewClient(Context c){
        context = c;
    }

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

    @Override
    public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
        pd = ProgressDialog.show(context, "", "pageload. Please wait...", true);
        view.loadUrl(url);  
        return true;
    } 
}

此代码有效,但初始加载时不会出现进度对话框。如果需要,请将此代码添加到类的构造函数中:

pd = ProgressDialog.show(context, "", "pageload. Please wait...", true);

You missed @Override annotation.

Here is right code:

class CustomWebViewClient extends WebViewClient {
    Context context;
    ProgressDialog pd = null;

    public CustomWebViewClient(Context c){
        context = c;
    }

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

    @Override
    public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
        pd = ProgressDialog.show(context, "", "pageload. Please wait...", true);
        view.loadUrl(url);  
        return true;
    } 
}

This code works, but progress dialog does't appears on initial loading. If you need it, add this code to the class' constructor:

pd = ProgressDialog.show(context, "", "pageload. Please wait...", true);
柳絮泡泡 2024-09-15 20:03:34

这确实有效,
你拼写 Finished 错了,你写了“onPageFinshed”

This does work,
you spelled Finished wrong, you wrote "onPageFinshed"

成熟稳重的好男人 2024-09-15 20:03:34
public class WebDialog extends Dialog
{

    static final int                      BLUE                  = 0xFF6D84B4;
    static final float[]                  DIMENSIONS_DIFF_LANDSCAPE =
                                                                    { 20, 60 };
    static final float[]                  DIMENSIONS_DIFF_PORTRAIT  =
                                                                    { 40, 60 };
    static final FrameLayout.LayoutParams   FILL                    = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
    static final int                      MARGIN                    = 4;
    static final int                      PADDING                   = 2;
    static final String                   DISPLAY_STRING            = "touch";

    private String                        mUrl;
//  private DialogListener                mListener;
    private ProgressDialog                mSpinner;
    private WebView                       mWebView;
    private LinearLayout                  mContent;
    private TextView                      mTitle;

    public WebDialog(Context context, String url)
    {
        super(context);
        mUrl = url;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        mSpinner = new ProgressDialog(getContext());
        mSpinner.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mSpinner.setMessage("Loading...");

        mContent = new LinearLayout(getContext());
        mContent.setOrientation(LinearLayout.VERTICAL);
        setUpTitle();
        setUpWebView();
        Display display = getWindow().getWindowManager().getDefaultDisplay();
        final float scale = getContext().getResources().getDisplayMetrics().density;
        int orientation = getContext().getResources().getConfiguration().orientation;
        float[] dimensions = (orientation == Configuration.ORIENTATION_LANDSCAPE) ? DIMENSIONS_DIFF_LANDSCAPE : DIMENSIONS_DIFF_PORTRAIT;
        addContentView(mContent, new LinearLayout.LayoutParams(display.getWidth() - ((int) (dimensions[0] * scale + 0.5f)), display.getHeight() - ((int) (dimensions[1] * scale + 0.5f))));
    }

    private void setUpTitle()
    {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        Drawable icon = getContext().getResources().getDrawable(R.drawable.ic_launcher);
        mTitle = new TextView(getContext());
        mTitle.setText("Website");
        mTitle.setTextColor(Color.WHITE);
        mTitle.setTypeface(Typeface.DEFAULT_BOLD);
        mTitle.setBackgroundColor(BLUE);
        mTitle.setPadding(MARGIN + PADDING, MARGIN, MARGIN, MARGIN);
//      mTitle.setCompoundDrawablePadding(MARGIN + PADDING);
//      mTitle.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
        mContent.addView(mTitle);
    }

    private void setUpWebView()
    {
        mWebView = new WebView(getContext());
        mWebView.setVerticalScrollBarEnabled(false);
        mWebView.setHorizontalScrollBarEnabled(false);
        mWebView.setWebViewClient(new WebDialog.DialogWebViewClient());
        mWebView.getSettings().setJavaScriptEnabled(true);

        System.out.println(" mURL = "+mUrl);

        mWebView.loadUrl(mUrl);
        mWebView.setLayoutParams(FILL);
        mContent.addView(mWebView);
    }

    private class DialogWebViewClient extends WebViewClient
    {

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

            return true;
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
        {
            super.onReceivedError(view, errorCode, description, failingUrl);
            WebDialog.this.dismiss();
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon)
        {
            super.onPageStarted(view, url, favicon);
            mSpinner.show();
        }

        @Override
        public void onPageFinished(WebView view, String url)
        {
            super.onPageFinished(view, url);
            String title = mWebView.getTitle();
            if (title != null && title.length() > 0)
            {
                mTitle.setText(title);
            }
            mSpinner.dismiss();
        }

    }
}
public class WebDialog extends Dialog
{

    static final int                      BLUE                  = 0xFF6D84B4;
    static final float[]                  DIMENSIONS_DIFF_LANDSCAPE =
                                                                    { 20, 60 };
    static final float[]                  DIMENSIONS_DIFF_PORTRAIT  =
                                                                    { 40, 60 };
    static final FrameLayout.LayoutParams   FILL                    = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
    static final int                      MARGIN                    = 4;
    static final int                      PADDING                   = 2;
    static final String                   DISPLAY_STRING            = "touch";

    private String                        mUrl;
//  private DialogListener                mListener;
    private ProgressDialog                mSpinner;
    private WebView                       mWebView;
    private LinearLayout                  mContent;
    private TextView                      mTitle;

    public WebDialog(Context context, String url)
    {
        super(context);
        mUrl = url;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        mSpinner = new ProgressDialog(getContext());
        mSpinner.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mSpinner.setMessage("Loading...");

        mContent = new LinearLayout(getContext());
        mContent.setOrientation(LinearLayout.VERTICAL);
        setUpTitle();
        setUpWebView();
        Display display = getWindow().getWindowManager().getDefaultDisplay();
        final float scale = getContext().getResources().getDisplayMetrics().density;
        int orientation = getContext().getResources().getConfiguration().orientation;
        float[] dimensions = (orientation == Configuration.ORIENTATION_LANDSCAPE) ? DIMENSIONS_DIFF_LANDSCAPE : DIMENSIONS_DIFF_PORTRAIT;
        addContentView(mContent, new LinearLayout.LayoutParams(display.getWidth() - ((int) (dimensions[0] * scale + 0.5f)), display.getHeight() - ((int) (dimensions[1] * scale + 0.5f))));
    }

    private void setUpTitle()
    {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        Drawable icon = getContext().getResources().getDrawable(R.drawable.ic_launcher);
        mTitle = new TextView(getContext());
        mTitle.setText("Website");
        mTitle.setTextColor(Color.WHITE);
        mTitle.setTypeface(Typeface.DEFAULT_BOLD);
        mTitle.setBackgroundColor(BLUE);
        mTitle.setPadding(MARGIN + PADDING, MARGIN, MARGIN, MARGIN);
//      mTitle.setCompoundDrawablePadding(MARGIN + PADDING);
//      mTitle.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
        mContent.addView(mTitle);
    }

    private void setUpWebView()
    {
        mWebView = new WebView(getContext());
        mWebView.setVerticalScrollBarEnabled(false);
        mWebView.setHorizontalScrollBarEnabled(false);
        mWebView.setWebViewClient(new WebDialog.DialogWebViewClient());
        mWebView.getSettings().setJavaScriptEnabled(true);

        System.out.println(" mURL = "+mUrl);

        mWebView.loadUrl(mUrl);
        mWebView.setLayoutParams(FILL);
        mContent.addView(mWebView);
    }

    private class DialogWebViewClient extends WebViewClient
    {

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

            return true;
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
        {
            super.onReceivedError(view, errorCode, description, failingUrl);
            WebDialog.this.dismiss();
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon)
        {
            super.onPageStarted(view, url, favicon);
            mSpinner.show();
        }

        @Override
        public void onPageFinished(WebView view, String url)
        {
            super.onPageFinished(view, url);
            String title = mWebView.getTitle();
            if (title != null && title.length() > 0)
            {
                mTitle.setText(title);
            }
            mSpinner.dismiss();
        }

    }
}
じ违心 2024-09-15 20:03:33

史蒂文& Sander,尝试在处理程序中关闭进度对话框,

如下所示:

  class pdHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
      if(pd != null)
      {
      pd.dismiss();
      pd = null;
      }
    }

然后在 onPageFinished 中调用处理程序:

 public void onPageFinshed(WebView view, String url){
        pdHandler.sendEmptyMessage(0);
    }

&你完成了!

Steven & Sander, try dismissing the progress dialog in a Handler

Something like this:

  class pdHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
      if(pd != null)
      {
      pd.dismiss();
      pd = null;
      }
    }

Then call your handler in onPageFinished:

 public void onPageFinshed(WebView view, String url){
        pdHandler.sendEmptyMessage(0);
    }

& you are done!

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