在android studio webview中删除页眉和页脚

时间:2017-09-14 21:53:16

标签: java android wordpress

我已经搜遍了所有的解决方案,但无法得到任何答案。我需要为我的WordPress网站构建一个webview应用程序,我需要一个实用的方法来帮助显示没有页眉和页脚的内容。我是Android工作室的新手。这就是我试过的

WebView custom;
String removeHeader1 = "javascript:document.getElementById('site-description').remove();";
String removeHeader2 = "javascript:document.getElementById('st').remove();";
String removeHeader3 = "javascript:document.getElementById('search').remove();";
String removeHeader4 = "javascript:document.getElementById('search-submit').remove();";

private ValueCallback<Uri> mUploadMessage;
private final static int FILECHOOSER_RESULTCODE = 1;

@Override
protected void onActivityResult(int requestCode, int resultCode,
                                Intent intent) {
    if (requestCode == FILECHOOSER_RESULTCODE) {
        if (null == mUploadMessage) return;
        Uri result = intent == null || resultCode != RESULT_OK ? null
                : intent.getData();
        mUploadMessage.onReceiveValue(result);
        mUploadMessage = null;
    }
}


@TargetApi(Build.VERSION_CODES.KITKAT)
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_custom_town);
    custom = new WebView(this);
    WebSettings settings = custom.getSettings();
    settings.setLoadWithOverviewMode(true);
    settings.setUseWideViewPort(true);
    settings.setBuiltInZoomControls(true);
    settings.setDisplayZoomControls(false);
    custom.getSettings().setJavaScriptEnabled(true);
    custom.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    custom.getSettings().setPluginState(WebSettings.PluginState.ON);
    custom.getSettings().setMediaPlaybackRequiresUserGesture(false);
    custom.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
    custom.setFocusable(true);
    custom.setFocusableInTouchMode(true);
    custom.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    custom.getSettings().setDatabaseEnabled(true);
    custom.getSettings().setAppCacheEnabled(true);
    custom.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    custom.evaluateJavascript(removeHeader1 , null);
    custom.evaluateJavascript(removeHeader2 , null);
    custom.evaluateJavascript(removeHeader3 , null);
    custom.evaluateJavascript(removeHeader4 , null);
    custom.loadUrl("https://linknigeria.com.ng/students");
    custom.setWebViewClient(new CustomTown.myWebClient());
    custom.setWebChromeClient(new WebChromeClient() {
        //The undocumented magic method override
        //Eclipse will swear at you if you try to put @Override here
        // For Android 3.0+
        public void openFileChooser(ValueCallback<Uri> uploadMsg) {

            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("image/*");
            CustomTown.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);

        }

        // For Android 3.0+
        public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("*/*");
            CustomTown.this.startActivityForResult(
                    Intent.createChooser(i, "File Browser"),
                    FILECHOOSER_RESULTCODE);
        }

        //For Android 4.1
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("image/*");
            CustomTown.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), CustomTown.FILECHOOSER_RESULTCODE);

        }

    });


    setContentView(custom);


}

public class myWebClient extends WebViewClient {

    //ProgressDialog
    ProgressDialog pd = null;

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {

        pd= new ProgressDialog(CustomTown.this);
        pd.setTitle("#BeFree");
        pd.setMessage("Loading...");
        pd.show();
        super.onPageStarted(view, url, favicon);
    }





    //Alert dialog
    AlertDialog alertDialog = null;

    @Override
    public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
        try {
            custom.stopLoading();
        } catch (Exception e) {
        }

        if (custom.canGoBack()) {
            custom.goBack();
        }

        custom.loadUrl("about:blank");
        AlertDialog alertDialog = new AlertDialog.Builder(CustomTown.this).create();
        alertDialog.setTitle("Error");
        alertDialog.setMessage("Check your internet connection and try again.");
        alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Try Again", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                finish();
                startActivity(getIntent());
            }
        });

        alertDialog.show();
        super.onReceivedError(webView, errorCode, description, failingUrl);
    }






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

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




}



@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction() == android.view.KeyEvent.ACTION_DOWN) {
        switch (keyCode) {
            case android.view.KeyEvent.KEYCODE_BACK:
                if (custom.canGoBack()) {
                    custom.goBack();
                } else {
                    finish();
                }
                return true;
        }
    }
    return super.onKeyDown(keyCode, event);
}

//flipscreen not loading again
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

}

可以在此处找到footer.php文件enter link description here header.php文件可以在enter link description here

找到

1 个答案:

答案 0 :(得分:0)

要对网页进行更改,需要启用webView以执行Javascript。 这是由接受布尔参数的API setJavaScriptEnabled()完成的。 True 用于启用javascript执行。

要删除页眉和页脚,需要使用相同的ID。这可以通过检查网页的JS文件来获得。

说标头的ID为page_header,页脚的标识为page_footer

将字符串变量分配给脚本javascript:document.getElementById('page_header').remove();

这将在该网页中找到ID,并由于remove()方法而将其删除。

然后我们只需要执行上面的脚本。 webview.evaluateJavaScript()帮助我们做到这一点。所需的参数是脚本和回调。如果不需要,回调可以为null。

因此要删除标题,

`String removeHeader = "javascript:document.getElementById('page_header').remove();";
if (Build.VERSION.SDK_INT >= 19) {
        webView.evaluateJavascript(script, callback);

} else
    webView.loadUrl(script, null);`