在Webview中添加进度对话框

时间:2013-05-31 03:35:16

标签: android webview dialog webpage progress

在网页浏览中加载网页时,我一直在尝试将进度对话框合并到我的应用中,因此不仅有空白屏幕。我知道这个问题随处可见,但我似乎无法找出对我有用的东西。我是编程和使用android的新手,所以任何信息都会有所帮助。下面是我现在拥有的代码。使用onPageStarted我得到了Bitmap的编译错误,我不确定是什么问题。感谢。

public class Grades extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);  


    //requesting system settings

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().requestFeature( Window.FEATURE_PROGRESS);
     WebView webview = new WebView(this);

     //web settings

    WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(true); 
        webSettings.setDisplayZoomControls(true);
        webSettings.setBuiltInZoomControls(true);


     setContentView(webview);

    //loads webpages in webview instead of launching browser

     webview.setWebViewClient(new WebViewClient() {
            ProgressDialog prDialog;
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                prDialog = ProgressDialog.show(Grades.this, null, "loading, please wait...");
                super.onPageStarted(view, url, favicon);
            }

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

   //loading webpage
     webview.loadUrl("page__link");
}

4 个答案:

答案 0 :(得分:30)

检查此代码,

    final ProgressDialog pd = ProgressDialog.show(OnlinePaymentActivity.this, "", "Please wait, your transaction is being processed...", true);

            mWebview  = (WebView)findViewById(R.id.webView1);

                mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript

                mWebview.getSettings().setLoadWithOverviewMode(true);
                mWebview.getSettings().setUseWideViewPort(true);
                mWebview.getSettings().setBuiltInZoomControls(true);


                mWebview.setWebViewClient(new WebViewClient() {
                    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                        Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
                    }

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


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

                            String webUrl = mWebview.getUrl();

                            }

}

                     });

                mWebview .loadUrl("www.google.com");

答案 1 :(得分:4)

带有进度对话框的Webview为我工作。完整的工作代码

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.yubrajpoudel.R;

/**
 * Created by yubraj on 12/25/15.
 */
public class NewsActivity extends AppCompatActivity {
    private WebView webView;
    ProgressDialog prDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_news);
        setSupportActionBar((Toolbar) findViewById(R.id.toolbar));

        webView = (WebView) findViewById(R.id.wv_news);
        webView.setWebViewClient(new MyWebViewClient());

        String url = "http://google.com/";
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        webView.loadUrl(url);


    }

    private class MyWebViewClient 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(NewsActivity.this);
            prDialog.setMessage("Please wait ...");
            prDialog.show();
        }

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

答案 2 :(得分:0)

试试这个,我希望,这就是你的目标

webview.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
            //Also you can show the progress percentage using integer value 'progress'
            if(!prDialog.isShowing()){
            prDialog = ProgressDialog.show(Grades.this, null, "loading, please wait...");
            }


            if (progress == 100&&prDialog.isShowing()) {
                prDialog.dismiss();
            }
        }
    });

答案 3 :(得分:0)

Kotlin Web视图和进度对话框对我有用。完整的工作代码

    var webView = web_view_privacy_policy
    webView?.settings?.javaScriptEnabled = true
    webView?.settings?.loadWithOverviewMode = true
    webView?.settings?.useWideViewPort= true

    webView.webViewClient = object : WebViewClient(){
        override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
            showProgress(getString(R.string.please_wait))
        }

        override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
            return true
        }

        override fun onPageFinished(view: WebView?, url: String?) {
            hideProgress()
        }
    }

    webView.loadUrl(ApiEndPoint.ENDPOINT_PRIVACY_POLICY)

web_view_privacy_policy是.xml中WebView的ID