在Android中实现javascript编程
jopen
12年前
运行如图:
示例是插入排序的例子。
主要的思路就是在webview中将编辑后的代码保存为一个html文件中,再用另一个WebView来加载。
直接上代码。
首先在manifest文件中添加几个权限:
<!-- 链接互联网权限 --> <uses-permission android:name="android.permission.INTERNET" /> <!-- 在SDCard中创建与删除文件权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <!-- 往SDCard写入数据权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <WebView android:id="@+id/webView" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <WebView android:id="@+id/swebView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:visibility="gone" /> </RelativeLayout>
主activity:
package com.example.webtest1; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import org.apache.http.util.EncodingUtils; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.app.Activity; import android.view.Menu; import android.view.MenuItem; import android.webkit.WebView; import android.widget.Toast; public class MainActivity extends Activity { private WebView mWebView; private WebView sWebView; private Handler mHandler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mWebView = (WebView) findViewById(R.id.webView); sWebView=(WebView) findViewById(R.id.swebView); sWebView.getSettings().setJavaScriptEnabled(true); mHandler=new Handler(); // 得到WebSettings对象,设置支持JavaScript参数 // 如果访问的页面中有JavaScript,则WebView必须设置支持JavaScript ,否则显示空白页面 mWebView.getSettings().setJavaScriptEnabled(true); sWebView.getSettings().setJavaScriptEnabled(true); mWebView.getSettings().setBuiltInZoomControls(true); // 加载URL mWebView.loadUrl("file:///android_asset/web/index.html"); //sWebView.loadUrl("file:///android_asset/result.html"); final class JSInterface{ public void clickOnAndroid(final String jsCode) { mHandler.post(new Runnable() { public void run() { //Toast.makeText(MainActivity.this, "测试调用java"+s, Toast.LENGTH_LONG).show(); mWebView.setVisibility(8); String html=""; html += "<head>"; html += "<body>"+"<script>"+jsCode+"</script>"+"</body>"; html += "</head>"; try { saveToSDCard("result.html",html); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } Toast.makeText(MainActivity.this,html, Toast.LENGTH_LONG).show(); sWebView.setVisibility(0); sWebView.loadUrl("file:///mnt/sdcard/result.html"); } }); } } mWebView.addJavascriptInterface(new JSInterface(), "demo"); } @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0,1,1,"back"); getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { // TODO Auto-generated method stub if(item.getItemId()==1) { mWebView.setVisibility(0); sWebView.setVisibility(8); } return super.onMenuItemSelected(featureId, item); } //写数据到SD中的文件 public void saveToSDCard(String filename, String content) throws Exception { //通过getExternalStorageDirectory方法获取SDCard的文件路径 File file = new File(Environment.getExternalStorageDirectory(), filename); //获取输出流 FileOutputStream outStream = new FileOutputStream(file); outStream.write(content.getBytes()); outStream.close(); } }
在asset/web中添加index.html
<html> <body> <h1>Js edit</h1> <form> <textarea id="code" name="code" cols="130" rows="15"> //InsertSort function InsertSort(arr) { var st = new Date(); var temp, j; for(var i=1; i<arr.length; i++) { if((arr[i]) < (arr[i-1])) { temp = arr[i]; j = i-1; do { arr[j+1] = arr[j]; j--; } while (j>-1 && (temp) < (arr[j])); arr[j+1] = temp; }//endif } status = (new Date() - st) + ' ms'; return arr; } var testArray=[3,324,5345,6546,134,5654,665]; document.write("<p>TestArray:</p>"); document.writeln(testArray.toString()); InsertSort(testArray); document.write("<p>After Sort:</p>"); document.writeln(testArray.toString()); </textarea> </form> <div id='b'><button onclick="window.demo.clickOnAndroid(document.getElementById('code').value)">Go!</button></div> </body> </html>
打完收工。
来自:http://blog.csdn.net/qp120291570/article/details/8621473