2016-07-29 89 views
0

目前我有这个代码,我想检索一个webview的内容,也同时删除显示的一部分的web视图,但我的代码根本没有做任何事情或显示任何错误,所以我在这里损失。目前,我指的是 this this我的代码。JavaScript不工作的WebView - Android

这是页面,我的WebView显示 link

这是我的代码“WebViewActivity.java`

public class WebViewActivity extends Activity{ 

TextToSpeech textToSpeech; 


@SuppressLint("JavascriptInterface") 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_webview); 

    final WebView webview = (WebView) findViewById(R.id.webView); 
    TextView contentView = (TextView) findViewById(R.id.contentView); 


    Intent intent = getIntent(); 
    String Address = intent.getStringExtra("URL"); 
    //Log.d("Valuer", value); 
    //tt1.setText(value); 

    class MyJavaScriptInterface 
    { 
     private TextView contentView; 

     public MyJavaScriptInterface(TextView aContentView) 
     { 
      contentView = aContentView; 
     } 

     @SuppressWarnings("unused") 
     @SuppressLint("JavascriptInterface") 

     public void processContent(String aContent) 
     { 
      final String content = aContent; 
      contentView.post(new Runnable() 
      { 
       public void run() 
       { 
        contentView.setText(content); 
       } 
      }); 
     } 
    } 

    webview.getSettings().setLoadsImagesAutomatically(true); 
    webview.addJavascriptInterface(new MyJavaScriptInterface(contentView), "INTERFACE"); 
    webview.getSettings().setJavaScriptEnabled(true); 
    webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); 
    webview.setWebViewClient(new WebViewClient() { 
     @Override 
     public void onPageFinished(WebView view, String url) 
     { 
      webview.loadUrl("javascript:window.INTERFACE.processContent(document.getElementsByClassName('western')[0].innerText);"); 
      webview.loadUrl("javascript:(function() { " + 
        "document.getElementsByTagName('section')[0].style.display=\"none\"; " + 
        "})()"); 
     } 
    }); 



    webview.loadUrl(Address); 
    //textToSpeech.speak("TALK", TextToSpeech.QUEUE_FLUSH, null); 


} 



} 

回答

2

试试这个, 通过加入这一行

 " webview.getSettings().setJavaScriptEnabled(true);" 

和围棋到您的“Proguard.rules”文件,并使该评论代码可用于JavaScript

# If your project uses WebView with JS, uncomment the following 
    # and specify the fully qualified class name to the JavaScript interface 
    # class: 
    #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 
    # public *; 
    #} 
+0

谢谢您的回复。只需删除#右键?我不需要编辑其他任何东西?我可以知道为什么android不会在默认设置中启用此功能吗?这是出于安全原因吗? –

+0

是的,只是删除“#”。 是的,我认为JavaScript是非常敏感的东西,它可能需要很大的负担来管理它,这就是为什么他们推荐这个。 –

+0

“为JavaScript接口类指定完全限定的类名”是什么意思? –