HTML5


使用 Android Mediaplayer 播放音訊。你需要在 HTML 檔案中呼叫的 Android 函式用 JavaScript 編寫。

WebView wv = (WebView) findViewById(R.id.webview);
wv.addJavascriptInterface(new WebAppInterface(this), "Android");

public class WebAppInterface {
   Context mContext;
   WebAppInterface(Context c) {
      mContext = c;
   }

   @JavascriptInterface
   public void showToast(String toast) {
      Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
   }
}

下面是我的 JavaScript

<input type = "button" value = "Demo" onClick = "showAndroidToast('Hello Android!')" />
<script>
   function showAndroidToast(toast) {
      Android.showToast(toast);
   }
</script>

更新於:2020 年 1 月 28 日

瀏覽量 518 次

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.