[Android] WebView interacts with JS

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 content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>