WebView is used to interact with the front end of the button, you can load the local HTML files, and Web pages and realize the function of interaction.
WebView can use Android native Javascriptinterface to communicate JS and Java via websetting.
Load local file: Webview.loadurl ("file:///android_asset/xxx.html");
Loading Web page: Webview.loadurl ("http://baidu.com");
Case: (webview load local HTML and implement with JS communication):
Code:
/*** * * * webview loading local files and implementing JS Click Effect * *@authorZQ **/ Public classMainactivityextendsActivity {PrivateWebView WebView; @Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Initview (); } Private voidInitview () {//TODO auto-generated Method Stub//Get WebView ControlWebView =(WebView) Findviewbyid (R.id.activity_webview); //get the settings for WebViewWebSettings websettings =webview.getsettings (); //set JavaScript to be available, this sentence is necessary, otherwise everything is futile.Websettings.setjavascriptenabled (true); //add JavaScript interface to WebViewWebview.addjavascriptinterface (NewJsinterface (), "control"); //loading HTML pages via WebViewWebview.loadurl ("file:///android_asset/l.html"); } Public classJsinterface {@JavascriptInterface Public voidshowtoast (String toast) {Toast.maketext (mainactivity. This, Toast, Toast.length_short). Show (); } Public voidLogFinalString msg) {Webview.post (NewRunnable () {@Override Public voidrun () {Webview.loadurl ("JavaScript log (" + "'" + msg + "'" + ")"); } }); } }}
JS file
function Showtoast (toast) { javascript:control.showToast (toast); } function log (msg) { consolse.log (msg); }
Androidmanifest.xml in Add-on permissions
<android:name= "Android.permission.INTERNET"/>< android:name= "Android.permission.ACCESS_NETWORK_STATE"/>
Code is not fully given, to download the source directly
source Download:http://download.csdn.net/detail/dickyqie/9710928
WebView load local HTML file and achieve click Effect