• webview和js交互


    public class WebViewDemo extends Activity {
    
        private static final String LOG_TAG = "WebViewDemo";
    
        private WebView mWebView;
    
        private Handler mHandler = new Handler();
    
        @Override
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.main);
            mWebView = (WebView) findViewById(R.id.webview);
    
            WebSettings webSettings = mWebView.getSettings();
            webSettings.setSavePassword(false);
            webSettings.setSaveFormData(false);
            webSettings.setJavaScriptEnabled(true);
            webSettings.setSupportZoom(false);
    
            mWebView.setWebChromeClient(new MyWebChromeClient());
    
            mWebView.addJavascriptInterface(new DemoJavaScriptInterface(), "demo");
    
            mWebView.loadUrl("file:///android_asset/demo.html");
        }
    
        final class DemoJavaScriptInterface {
    
            DemoJavaScriptInterface() {
            }
    
            /**
             * This is not called on the UI thread. Post a runnable to invoke
             * loadUrl on the UI thread.
             */
            public void clickOnAndroid() {
                mHandler.post(new Runnable() {
                    public void run() {
                        mWebView.loadUrl("javascript:wave()");
                    }
                });
    
            }
        }
    
        /**
         * Provides a hook for calling "alert" from javascript. Useful for
         * debugging your javascript.
         */
        final class MyWebChromeClient extends WebChromeClient {
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                Log.d(LOG_TAG, message);
                result.confirm();
                return true;
            }
        }
    }


    demo.html
    <html>
        <script language="javascript">
            /* This function is invoked by the activity */
            function wave() {
                alert("1");
                document.getElementById("droid").src="android_waving.png";
                alert("2");
            }
        </script>
        <body>
            <!-- Calls into the javascript interface for the activity -->
            <a onClick="window.demo.clickOnAndroid()"><div style="80px;
                margin:0px auto;
                padding:10px;
                text-align:center;
                border:2px solid #202020;" >
                    <img id="droid" src="android_normal.png"/><br>
                    Click me!
            </div></a>
        </body>
    </html>


    ①android如何调用js。
    调用形式:
    mWebView.loadUrl("javascript:wave()");加载js中的wave()方法

    ②js如何调用android。
    调用形式:
    <a onClick="window.demo.clickOnAndroid()">

    代码中的“demo”是在android中指定的调用名称,即
    mWebView.addJavascriptInterface(new DemoJavaScriptInterface(), "demo");

    代码中的clickOnAndroid()是“demo”对应的对象:new DemoJavaScriptInterface() 中的一个方法。

    ③双向交互。



  • 相关阅读:
    白兔的字符串(字符串hash+模拟map)
    [TJOI2013]单词(AC自动机+前缀和维护)
    [SDOI2014]数数(ac自动机+数位DP)
    阿狸的打字机(AC自动机+dfs序 + 维护区间值)
    string(AC自动机 在线询问转离线询问)
    E
    JMX超详细解读
    快速生成100万数据人员信息数据
    聊聊spring之bean对象的实例化过程
    聊聊spring之贯穿全局的重要对象BeanDefinition
  • 原文地址:https://www.cnblogs.com/wwccyy66/p/6194952.html
Copyright © 2020-2023  润新知