加载Specefic Div WebView

时间:2017-02-15 20:57:13

标签: java android html webview

我有一个网站 - > https://docs.google.com/viewer?url=http://pzstiz.swiebodzin.pl/zastepstwa/PZSTiZ_zastepstwa.pdf

在我的应用程序中使用webView。 我想加载一个specefic div(图片来自这个pdf)

<div class="ndfHFb-c4YZDc-cYSp0e-DARUcf-PLDbbf ndfHFb-c4YZDc-AHmuwe-wcotoc-zTETae"></div>

有我的代码:

package musial.pzstis.com.pzstiz;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;


public class Main2Activity extends Activity {

    WebView myWebView3;
    ProgressBar progressBar3;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        myWebView3 = (WebView) findViewById(R.id.webView);
        progressBar3 = (ProgressBar) findViewById(R.id.progressBar7);

        myWebView3.setWebViewClient(new myWebClient());
        myWebView3.getSettings().setJavaScriptEnabled(true);
        WebSettings webSettings = myWebView3.getSettings();
        myWebView3.loadUrl("https://docs.google.com/viewer?url=http://pzstiz.swiebodzin.pl/zastepstwa/PZSTiZ_zastepstwa.pdf");
        webSettings.setBuiltInZoomControls(true);
        myWebView3.setInitialScale(285);
        myWebView3.getSettings().setUseWideViewPort(true);
        myWebView3.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        myWebView3.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
    }

    public class myWebClient extends WebViewClient
    {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub
            progressBar3.setVisibility(View.VISIBLE);
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);

            progressBar3.setVisibility(View.GONE);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView3.canGoBack()) {
            myWebView3.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}

1 个答案:

答案 0 :(得分:0)

您可以webview加载javascript code

首先获取所有元素

var all = document.getElementsByTagName(“*”);

然后循环

for (var i=0, max=all.length; i < max; i++) {
     // Do something with the element here
     // In your condition,you can do like this:
     if(all[i].classname != 'ndfHFb-c4YZDc-cYSp0e-DARUcf-PLDbbf ndfHFb-c4YZDc-AHmuwe-wcotoc-zTETae')

         all[i].style.display='none';

}

构造一个字符串

 String s = "xxxx"// copy above code 

最后

 webView.load(s);