1
A
回答
0
好吧,這很容易,但你需要做的部分,你需要JavaScript。
1.-在您的webview中啓用JavaScript。
web.getSettings().setJavaScriptEnabled(true); //"web" is the object of type WebView
2.-在WebView和Html之間創建一個JavaScript接口。
public class JavaScriptInterface {
private Handler myHandler = new Handler();
public void getParagraph(final String paragraph){
myHandler.post(new Runnable() {
@Override
public void run() {
//Do something with the String
}
});
}
}
注:方法運行裏面,你會添加任何你需要從HTML中檢索到的字符串處理。如果您要從其他活動使用相同的行爲,則可以在創建WebView的類中創建此類,也可以將其作爲單獨的類創建。
3.-將接口發送到WebView。
web.addJavascriptInterface(new JavaScriptInterface(), "android");
/* In this case "android" is the name that you will use from the Html to call
your methods if is not too clear yet, please keep reading */
4.-您需要將onClick事件處理程序添加到您的每個P標籤。
<p onclick="android.getParagraph(this.innerText);">Text inside the paragraph</p>
/*android was the name we set for the interface in step 3, getParagraph is the name
of the method created on step2,"this.innerText" retrieves the text inside the paragraph*/
注:所有你對我的例子中看到的名稱可以改變,但是如果你改變Java類的名字記得更改HTML所有呼叫
相關問題
- 1. Android將觸摸事件傳遞給WebView的內容
- 2. 如何通過觸摸UIView將內容加載到WebView中
- 3. Android WebView中的觸摸事件
- 4. 在Android中禁用WebView觸摸事件
- 5. android webview中無響應的觸摸
- 6. Android:用短觸摸代替WebView long touch
- 7. Android WebView鏈接觸摸區域
- 8. Android WebView觸摸事件鏈接
- 9. Android內三角觸摸
- 10. 在webview上檢測觸摸
- 11. Android - 檢測電容式觸摸屏上的觸摸壓力?
- 12. Android的webview內容超過了webview
- 13. android webview加密內容
- 14. android webview內容不可選
- 15. webview內容的限制android
- 16. Android WebView內容縮放
- 17. WebView下的不可觸摸項目
- 18. 適合webview內的網頁內容(Android)
- 19. Android雖然觸摸和觸摸發佈
- 20. 只在視圖內觸摸 - android
- 21. 從android內核模擬觸摸事件
- 22. webview內容高度
- 23. Android:通過webview加載的url內的下拉菜單觸摸事件
- 24. WebView中的WebView停止滾動/觸摸事件
- 25. WebView內容
- 26. 在uiview之上觸摸不到webView
- 27. Swift 3的webview觸摸事件
- 28. Webview沒有響應觸摸事件
- 29. WebView的方向 - 單點觸摸
- 30. 在webView中觸摸事件 - JavaFX
我看過的文檔。但是我只使用過一次這種方法,而且它稍微有些不同,我想也許額外的類型是'UNKNOWN_TYPE'時可能包含段落字符串。但沒有時間來測試這個理論,所以我想我會與OP分享它,以防它可以幫助他們。任何這看起來像一個很好的解決方案感謝分享。 – FoamyGuy
您的歡迎[@FoamyGuy](http://stackoverflow.com/users/507810/foamyguy),我知道你做了好事情:D –