Implementation of interaction between WebView and JS in Android
Get WebView object
Call the getsettings () method of the WebView object to get the websettings object
Call setjavascriptenabled() method of websettings object, set JS available, parameter: Boolean
When judging whether JS is supported or not, do not use alert (), which does not work by default. You can use document. Write () to test first
Call addjavascriptinterface (obj, interfacename) method of WebView object and add JS interface. Parameters: object object, string interface name (alias of this object in JS)
Define an inner class myjavascript
Define a method showtoast() to display toast. If the API version is greater than 17, it needs to be annotated @ javascriptinterface
Java code:
JS code:
The above content is the implementation method of WebView and JS interaction in Android introduced by Xiaobian. I hope it can help you!