1. 程式人生 > >安卓 html

安卓 html

public class JsActivity extends Activity {

	private WebView mWebView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_js_activity);

		mWebView = (WebView) findViewById(R.id.webView);

		WebSettings webSettings = mWebView.getSettings();
		webSettings.setSaveFormData(false);
		webSettings.setJavaScriptEnabled(true);
		webSettings.setSupportZoom(false);
		webSettings.setJavaScriptCanOpenWindowsAutomatically(true);

		mWebView.addJavascriptInterface(new MyClass(), "mine");
		mWebView.loadUrl("file:///android_asset/main.html");
	}

	final class MyClass{
		MyClass() {
		}

		@JavascriptInterface
		public String test() {
			return "hello world";
		}
	}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <WebView
        android:id="@+id/webView"
        android:layout_width="fill_parent"
        android:layout_height="match_parent" />

</LinearLayout>
<html>
<script language="javascript">
	function btnclick() {
		var value = window.mine.test();
		document.getElementById("input1").value = value;
	}
</script>
<body>
	<br>
	<input type="button" value="input test" onclick="btnclick()" />
	<br>
	<input type="text" id="input1" />
	<br>
</body>
</html>