在网站完成加载之前调用onPageFinished方法?

时间:2014-04-18 13:44:02

标签: android webview

似乎我做了一些不准确的事情,或者我错过了有关public class myWebViewClient课程和public void onPageFinished(WebView view, String url)的内容。最初和开始时我尝试启动www.yahoo.com,在完成加载后,我从public void onPageFinished(WebView view, String url)内部发出一个吐司,作为对我的指示,www.yahoo.com的加载完成了,直到这里的每件事都很好。在我的RelativeLayout里面,我有一个按钮和edittext,我的webview就在它们上面。拥有edittext的目的是让用户输入他们的网站,然后点击按钮,输入的网站应该启动,我希望看到public void onPageFinished(WebView view, String url)内的吐司在新的后再次被触发网站完成加载。但是,一旦用户在edittext字段中输入新网站,并且在他们按下go_button之后,即使网站加载也会立即触发,并在同一网站完成加载后再次触发。实际上,我希望只有在每个网站完成加载后才能解雇吐司,但是我发生的事情并非如此。请提供建议,以防止出现此类问题。

JavaCode:

OnClickListener btnGoListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        webView00.loadUrl(etUrl.getText().toString());
    }
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_web_page00);

    webView00 = (WebView) findViewById(R.id.webView00);
    btnGo = (Button) findViewById(R.id.go_btn);
    etUrl = (EditText) findViewById(R.id.url_edittext);

    webView00.getSettings().setJavaScriptEnabled(true);
    webView00.setWebViewClient(new myWebViewClient());
    webView00.setWebChromeClient(new myWebChromeClient());

    btnGo.setOnClickListener(btnGoListener);
    webView00.loadUrl("http://google.com");
}

public class myWebChromeClient extends WebChromeClient {

}

public class myWebViewClient extends WebViewClient {        
    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        Toast.makeText(getBaseContext(), "Loading Finished", Toast.LENGTH_LONG)
                       .show();
        }
    };

3 个答案:

答案 0 :(得分:1)

来自文档:

When onPageFinished() is called, the rendering picture may not be updated yet. 
To get the notification for the new Picture, use onNewPicture(WebView, Picture).

http://developer.android.com/reference/android/webkit/WebViewClient.html#onPageFinished(android.webkit.WebView,java.lang.String)

我建议你从WebChromeClient实现方法onProgressChanged

文档:

http://developer.android.com/reference/android/webkit/WebChromeClient.html#onProgressChanged(android.webkit.WebView,int)

答案 1 :(得分:0)

尚未尝试使用Chrome客户端,因此请注意一下,也许您可​​以在它们之间进行无缝更改。

尽管如此,我认为这样的事情应该有效:

OnClickListener btnGoListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        webView00.loadUrl(etUrl.getText().toString());
        webView00.setWebViewClient(new WebViewClient() {

            public void onPageFinished(WebView view, String url) {
                Toast.makeText(getBaseContext(), "Loading Finished 2", Toast.LENGTH_LONG)
                .show();
            }
        });
    }
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_web_page00);

    webView00 = (WebView) findViewById(R.id.webView00);
    btnGo = (Button) findViewById(R.id.go_btn);
    etUrl = (EditText) findViewById(R.id.url_edittext);

    webView00.getSettings().setJavaScriptEnabled(true);

    btnGo.setOnClickListener(btnGoListener);
    webView00.loadUrl("http://google.com");

    //webView00.setWebViewClient(new myWebViewClient());
    //webView00.setWebChromeClient(new myWebChromeClient());
    webView00.setWebViewClient(new WebViewClient() {

        public void onPageFinished(WebView view, String url) {
            Toast.makeText(getBaseContext(), "Loading Finished 1", Toast.LENGTH_LONG)
            .show();
        }
    });
}

答案 2 :(得分:0)

我注意到,如果 webview 重新呈现已加载的页面,有时会多次调用 onPageFinished。在重新加载时,webview 将首先呈现 about:blank 并调用 onPageFinished,然后加载实际网页并再次调用 onPageFinished。顺序是:

  1. 加载www.google.com
  2. onPageFinished 被调用
  3. 再次加载“www.google.com”
  4. onPageFinished 被调用。网址是 about:blank
  5. onPageFinished 被调用。网址是 www.google.com

要在第 4 步中解决此问题,您可以检查传递给 urlonPageFinished 参数是否是您关心的参数:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       ...
    
        webView00.loadUrl(myWebPage);
    }

    
    public class myWebViewClient extends WebViewClient {        
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, URL);
            if(url == myWebPage) {
                Toast.makeText(getBaseContext(), "Loading Finished", Toast.LENGTH_LONG)
                           .show();
            }
        };