[android] WebView與Js互動

來源:互聯網
上載者:User

標籤:

擷取WebView對象

調用WebView對象的getSettings()方法,擷取WebSettings對象

調用WebSettings對象的setJavaScriptEnabled()方法,設定js可用,參數:布爾值

在判斷是否支援js的時候,不要用alert(),預設不起作用,可以先用document.write()測試

 

調用WebView對象的addJavascriptInterface(obj, interfaceName)方法,添加js介面,參數:Object對象,String介面名稱(這個對象在js中的別名)

定義一個內部類MyJavascript

定義一個方法showToast(),顯示多士,api版本大於17需要加註解@JavascriptInterface

 

java代碼:

package com.tsh.mywebview;import android.annotation.SuppressLint;import android.app.Activity;import android.app.ProgressDialog;import android.graphics.Bitmap;import android.os.Bundle;import android.view.KeyEvent;import android.view.Menu;import android.view.MenuItem;import android.view.Window;import android.webkit.JavascriptInterface;import android.webkit.WebSettings;import android.webkit.WebView;import android.webkit.WebViewClient;import android.widget.Toast;public class MainActivity extends Activity {    private WebView webview;    private ProgressDialog pd;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.activity_main);                pd=new ProgressDialog(this);        pd.setMessage("正在載入...");                        //webview的簡單設定        webview=(WebView) findViewById(R.id.wv_internet);        //http://100.65.187.106/test.php        webview.loadUrl("http://100.65.187.106/test.php");        WebSettings websettings=webview.getSettings();        websettings.setSupportZoom(true);        websettings.setBuiltInZoomControls(true);                //js互動        new MyJavascript().showToast("111");        websettings.setJavaScriptEnabled(true);        webview.addJavascriptInterface(new MyJavascript(), "Android");        webview.loadUrl("javascript:documentWrite(‘測試‘)");                webview.setWebViewClient(new WebViewClient(){            @Override            public void onPageStarted(WebView view, String url, Bitmap favicon) {                pd.show();            }            @Override            public void onPageFinished(WebView view, String url) {                pd.dismiss();            }        });            }    //暴露給js的功能介面    public class MyJavascript{        //顯示多士        // 如果target 大於等於API 17,則需要加上如下註解        @JavascriptInterface        public void showToast(String text) {            Toast.makeText(MainActivity.this, text, 1).show();        }        //顯示loading        @JavascriptInterface        public void showProgressDialog(String text) {            pd.setMessage(text);            pd.show();        }    }    //後退鍵    @Override    public boolean onKeyDown(int keyCode, KeyEvent event) {        if(keyCode==KeyEvent.KEYCODE_BACK&&webview.canGoBack()){            webview.goBack();            return true;        }        return super.onKeyDown(keyCode, event);    }    //菜單鍵    @Override    public boolean onCreateOptionsMenu(Menu menu) {        menu.add(0, 0, 0, "重新整理");        menu.add(0, 0, 1, "後退");        menu.add(0, 0, 2, "前進");        return super.onCreateOptionsMenu(menu);    }    //菜單點擊事件    @Override    public boolean onOptionsItemSelected(MenuItem item) {        switch (item.getOrder()) {        case 0:            webview.reload();            break;        case 1:            if(webview.canGoBack()){                webview.goBack();            }            break;        case 2:            if(webview.canGoForward()){                webview.goForward();            }            break;        }        return super.onOptionsItemSelected(item);    }}

 

js代碼:

 

<html><head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>    <title>測試android程式</title></head><body>    測試android和js互動    <br/>    <button onClick="showToast()">顯示多士</button>    <br/>    <button onClick="showProgressDialog()">顯示loading</button><script type="text/javascript">function showToast(){    Android.showToast("顯示多士");}function showProgressDialog(){    Android.showProgressDialog("顯示進度條");}</script></body></html>

 

 

[android] WebView與Js互動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.