2014-02-07 62 views
1

如何從android中的webview中獲取文本? 下面是我的一些代碼:如何從android中的webview中獲取文本?

webView = (WebView)findViewById(R.id.wv_memo); 
    webView.requestFocus(View.FOCUS_DOWN); 
    webView.setOnTouchListener(new View.OnTouchListener() 
    { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) 
     { 
      switch (event.getAction()) 
      { 
       case MotionEvent.ACTION_DOWN: 
       case MotionEvent.ACTION_UP: 
        if (!v.hasFocus()) 
        { 
         v.requestFocus(); 
        } 
        break; 
      } 
      return false; 
     } 
    }); 
    WebSettings webSettings = webView.getSettings(); 
    webSettings.setBuiltInZoomControls(true); 
    webSettings.setJavaScriptEnabled(true); 
    webView.addJavascriptInterface(new MyJavaScriptInterface(this), "HtmlViewer"); 


    webView.loadDataWithBaseURL(null,"<div contenteditable=\"true\" style=\"height:220px;\"></div>", "text/html", "utf-8", "about:blank"); 
+0

請幫忙!!!我真的需要答案才能完成我的項目。 :) – phathsan

+0

你可以使用jsoup html解析器提取div標籤的內容 – Raghunandan

+0

謝謝Raghunandan,但你是更具體還是可以指導我們一些編碼? – phathsan

回答

0

你應該有獲取HTML從這個類

class MyJavaScriptInterface 
{ 

     private Context ctx; 

     MyJavaScriptInterface(Context ctx) 
     { 
      this.ctx = ctx; 
     } 

     public void showHTML(String html) 
     { 
      String htmlTags = html; 
     } 
} 

標籤的值在上面的代碼中,showHTML()方法中的變量HTML標籤有全文HTML你要。

0

添加的接口(MyJavaScriptInterface)中的方法都沒有用@android.webkit.JavascriptInterface註解;它們在API 17中將不可見。WebView.addJavascriptInterface出於安全原因,不應使用minSdkVersion調用< 17:JavaScript可以使用反射來操縱應用程序。

相關問題