android中引用javascript和在javascript中引用java的簡單例子,androidjavascript
android中引用javascript和在javascript中引用java的簡單例子
在android中通過微webView是可以載入javascript代碼的,與其說是javascript不如說是載入網頁,其實就是html和javascript的結合等,通過html和javascript也可以建立安卓應用,因為android和javascript可以相互調用,下面是我介紹的一個簡單的例子,大家可以參考。歡迎和大家一起交流。
//允許JavaScript執行
webSettings.setJavaScriptEnabled(true);
// 添加一個對象, 讓javascript可以訪問該對象的方法,
myWebView.addJavascriptInterface(new WebAppInterface(this),
"myInterfaceName");
//java中調用javascript中的方法
myWebView.loadUrl("javascript:myFunction()");
具體的大家看程式碼分析吧,這個簡單的列子其實很容易明白的
package com.mlf.javascripttest;import android.os.Bundle;import android.annotation.SuppressLint;import android.app.Activity;import android.content.Context;import android.view.View;import android.view.View.OnClickListener;import android.webkit.WebChromeClient;import android.webkit.WebSettings;import android.webkit.WebView;import android.webkit.WebViewClient;import android.webkit.JsResult;import android.widget.Button;import android.widget.Toast;//@SuppressLint("SetJavaScriptEnabled")@SuppressLint({ "SetJavaScriptEnabled", "JavascriptInterface" })public class MainActivity extends Activity {private WebView myWebView;private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myWebView=(WebView) findViewById(R.id.javascriptWebview); button=(Button) findViewById(R.id.uttonId); WebSettings webSettings=myWebView.getSettings(); //允許JavaScript執行 webSettings.setJavaScriptEnabled(true); webSettings.setDefaultTextEncodingName("GBK"); myWebView.setWebViewClient(new WebViewClient()); myWebView.setWebChromeClient(new WebChromeClient() { @Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { // TODO Auto-generated method stub return super.onJsAlert(view, url, message, result); } }); // 添加一個對象, 讓javascript可以訪問該對象的方法, myWebView.addJavascriptInterface(new WebAppInterface(this), "myInterfaceName"); // 載入頁面:本地html資源檔,放在assets檔案夾下 myWebView.loadUrl("file:///android_asset/javascripttest.html"); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub //java中調用javascript中的方法 myWebView.loadUrl("javascript:myFunction()"); } }); } class WebAppInterface{ Context mContext; WebAppInterface(Context c){ mContext=c; } public void showToast(String toast){ Toast.makeText(mContext, toast, Toast.LENGTH_LONG).show(); } } }
javascript xml檔案
<html><head><h1>This is a HTML Page</h1><!-- JavaScript指令碼,主要包括了按鈕要執行的函數,顯示對話方塊等 --><script type="text/javascript"> //JavaScript方法,彈出對話方塊顯示資訊 function myFunction() { alert("Hello World!"); } function onAlert() { console.log("onAlert method");//顯示調試資訊 alert("This is a alert sample from html"); } function onConfirm() { console.log("onConfirm method"); var b = confirm("are you sure to login?"); alert("your choice is " + b); } function onPrompt() { console.log("onPrompt method"); var b = prompt("please input your password", "aaa"); alert("your input is " + b); } //調用綁定的Java對象的方法,即調用Android代碼顯示對話方塊 function showAndroidToast(toast) { console.log("showAndroidToast method"); myInterfaceName.showToast(toast);//注意此處的myInterfaceName要和外部傳入的名字一致,大小寫正確 }</script></head><body> <p> <!-- 前四個按鈕調用JS函數 --> JavaScript函數調用 <br /> <button onclick="myFunction()">點擊這裡!</button> <br /> <input type="button" value="alert" onclick="onAlert()" /> <br /> <input type="button" value="confirm" onclick="onConfirm()" /> <br /> <input type="button" value="prompt" onclick="onPrompt()" /><br /> <!-- 上面用了兩種定義按鈕的方式,效果一樣的 --> </p> <p> <!-- 這個Say hello 按鈕調用Android代碼中的方法 --> 用JavaScript按鈕調用Android代碼 <br /> <input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" /> </p> <a href="http://www.google.com" />Google </a></body></html>
布局檔案xml
<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" > <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="javascript和android相互調用" android:textSize="20dp" android:gravity="center" android:id="@+id/textView1"/> <WebView android:layout_below="@+id/textView1" android:id="@+id/javascriptWebview" android:layout_width="match_parent" android:layout_height="380dp"/> <Button android:id="@+id/uttonId" android:layout_below="@+id/javascriptWebview" android:layout_width="match_parent" android:layout_height="50dp" android:textSize="20dp" android:gravity="center" android:text="android調用javascript"/></RelativeLayout>
謝謝大家參考借鑒,有機會多多交流!歡迎提出疑問,或有新的領先技術學習!