2016-07-29 56 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接口類指定完全限定的類名」是什麼意思? –