从webview中删除页眉和页脚

时间:2015-07-13 06:57:27

标签: android header android-webview jsoup footer

我想在将内容加载到Web视图之前删除页眉和页脚。此代码无效。

package com.wiseme;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.GridView;

@SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity {

    GridView grid;
    public WebView webView;
    public int pos;

    String[] desc = { "Who We Are", "What We Do", "Entrepreneur",
            "Scholarship", "Admission", "Internship", "Industrial Visit",
            "Project", "Buy or Sell Projects", "Free Training",
            "College Registration", "Information", "Feedback", "Contact" };

    int[] imageId = { R.drawable.dummy, R.drawable.dummy, R.drawable.dummy,
            R.drawable.dummy, R.drawable.dummy, R.drawable.dummy,
            R.drawable.dummy, R.drawable.dummy, R.drawable.dummy,
            R.drawable.dummy, R.drawable.dummy, R.drawable.dummy,
            R.drawable.dummy, R.drawable.dummy

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activitymain);

        GridDesign adapter = new GridDesign(MainActivity.this, desc, imageId);
        grid = (GridView) findViewById(R.id.grid);
        grid.setAdapter(adapter);
        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                pos = position;
                Intent intent = new Intent(MainActivity.this, webView.class);
                startActivity(intent);
            }
        });

    }



    @Override
    public void onBackPressed() {
        new AlertDialog.Builder(this)
                .setMessage("Are you sure you want to exit?")
                .setCancelable(false)
                .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                MainActivity.this.finish();

                            }
                        }).setNegativeButton("No", null).show();
    }

}

网络视图类

package com.wiseme;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class webView extends MainActivity {

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        WebView webView;
        webView = new WebView(this);

        WiseWeWebClient myWebClient = new WiseWeWebClient();
        webView.setWebViewClient(myWebClient);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);




        switch (pos) {
        case 0:
            webView.loadUrl("http://wisewe.com/who-we-are/");
            break;
        case 1:
            webView.loadUrl("http://wisewe.com/what-we-do/ ");
            break;
        case 2:
            webView.loadUrl("http://wisewe.com/entrepreneur/ ");
            break;
        case 3:
            webView.loadUrl("http://wisewe.com/scholarship/");
            break;
        case 4:
            webView.loadUrl("http://wisewe.com/college-admission/ ");
            break;
        case 5:
            webView.loadUrl("http://wisewe.com/internship/");
            break;
        case 6:
            webView.loadUrl("http://wisewe.com/industrial-visit/");
            break;
        case 7:
            webView.loadUrl("http://wisewe.com/project/ ");
            break;
        case 8:
            webView.loadUrl("http://wisewe.com/sell-projects/");
            break;
        case 9:
            webView.loadUrl("http://wisewe.com/free-training/");
            break;
        case 10:
            webView.loadUrl("http://wisewe.com/college-registration/");
            break;
        case 11:
            webView.loadUrl("http://wisewe.com/branch/wise-we/");
            break;
        case 12:
            webView.loadUrl("http://wisewe.com/branch/feedback/");
            break;
        case 13:
            webView.loadUrl("http://wisewe.com/contact-us/");
            break;
        }
        setContentView(webView);
    }

    @Override
    public void onBackPressed() {
        webView.this.finish();
    }
}

WiseWeWebClient

package com.wiseme;

import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WiseWeWebClient extends WebViewClient {

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

    @Override
    public void onPageFinished(WebView view, String url) {
        view.loadUrl("javascript:var footer = document.getElementById('footer'); footer.parentNode.removeChild(footer); var header = document.getElementById('header-full'); header.parentNode.removeChild(header);");
    }

}

标题.......

enter image description here

页脚......

enter image description here

这些都更新到最新。我仍然无法删除页眉和页脚。

2 个答案:

答案 0 :(得分:1)

根据您的屏幕截图,您使用了多个Jsoup Library。只使用其中一个。 使用this link下载Jsoup Library

答案 1 :(得分:1)

根据您的屏幕截图和网站,这是一个web / html问题:您想要删除页面的一些DOM元素。如果您想要移动设备上的页眉/页脚而不是您的应用程序,则必须在加载页面时运行一些javascript:

  1. 创建自定义WebViewClient以收听onPageFinished

    public class WiseWeWebClient extends WebViewClient {
    
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    
        @Override
        public void onPageFinished(WebView view, String url) {
            view.loadUrl("javascript:var footer = document.getElementById(\"footer\"); footer.parentNode.removeChild(footer); var header = document.getElementById(\"header-full\"); header.parentNode.removeChild(header);");
        }
    
    }
    
  2. 将WebClient设置为您的webview

    WiseWeWebClient myWebClient = new WiseWeWebClient();
    webview.setWebViewClient(myWebClient);
    WebSettings webSettings = webview.getSettings();
    webSettings.setJavaScriptEnabled(true);