2011-07-27 26 views
0

當我點擊顯示HTML文件的webview(該文件的主要部分是字符串)時,我想將書籤功能添加到我的應用中,我想要捕獲顯示在屏幕上的內容的第一行。任何人都知道答案?如何獲得在webview上點擊的webview內容

非常感謝。

-Shawn

回答

0

如果有鏈接到網頁視圖和你想要做特定的動作時,此鏈接,用戶點擊一下,你必須使用下面的代碼趕上鍊接點擊:

在某處您的活動代碼(通常在onCreate方法):

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState, R.layout.news_details); 
    ... 
    mWebView.setWebViewClient(new MyWebViewClient()); 
    ... 
} 

而且WebViewClient類:

class MyWebViewClient extends WebViewClient { 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     if (DEBUG) { 
      Log.d(TAG, "shouldOverrideUrlLoading url= " + url); 
     } 
     if (the url is like you want) { 
      // TODO: add the code to do what you need to do with the url 
      // the webview should not do anything with this link. 
      return true; 
     } else { 
      // let the webview normally handle the link 
      return false; 
     } 
    } 
} 

如果你想要做的是獲取WebView的實際顯示內容,那麼沒有API可以做到這一點。

對這些帖子一看:

Is it possible to get the HTML code from WebView

​​

兩個重定向這個網站上:

http://lexandera.com/2009/01/extracting-html-from-a-webview/

1

可以識別頁面的部分內容它包含img標籤或不包含 使用此A web視圖的PI單擊here

WebView.HitTestResult hr = ((WebView)v).getHitTestResult(); 
int i=hr.getType() ; 

,並使用這個類的int值中的內容

希望它能幫助