2017-09-14 146 views
-4

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

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

+0

欢迎堆栈溢出。你已经尝试过这么做了什么?请回顾[预计需要多少研究工作?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users)。堆栈溢出不是一种编码服务。预计您会在发布之前研究您的问题,并尝试亲自编写代码***。如果您遇到* *特定的*,请返回并包含[最小,完整和可验证示例](https://stackoverflow.com/help/mcve)以及您尝试的摘要。 – FluffyKitten

回答

0

要更改网页,web视图需要启用执行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);` 
+0

我不知道你的意思是执行“evaluateJavascript()”对不起,我是一个新手。我可以得到一个解释性样本吗? – Abiola

+0

@Abiola方法'evaluateJavascript()'是webview类提供的API。已更新答案 – aditya

+0

它说只允许kitkat ...可悲...它只能为KitKat + – Abiola