且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Android上执行JavaScript?

更新时间:2023-02-15 14:48:50

您可以使用web视图继承View类。做一个XML标记,并使用 findViewById()功能在活动中使用。但是,使用JavaScript,你可以创建包含JavaScript的code一个HTML文件。这个例子blelow可能的帮助。

You can use Webview which inherits View class. Make an XML tag and use findViewById() function to use in the activity. But to use the JavaScript, you can make a HTML file containing the JavaScript code. The example blelow might help.

Webview browser=(Webview) findViewById(R.main.browser); //if you gave the id as browser
browser.getSettings().setJavaScriptEnabled(true); //Yes you have to do it
browser.loadUrl("file:///android_asset/JsPage.html"); //If you put the HTML file in asset folder of android

记住,JS将运行在web视图,而不是在本地环境中,因此,你可能会遇到在模拟器上滞后或缓慢的FPS。但是用在实际的手机的时候,在code可以跑得快,这取决于有多快你的电话。

Remember that the JS will run on WebView, not in native environment, thus you might experience a lag or slow FPS in emulator. However when using on an actual phone, the code may run fast, depending on how fast is your phone.