• android js 互动事件


    引用:http://blog.163.com/ws_wishao/blog/static/173046963201112195020124/

    ERROR/InputDispatcher(61): channel '40723150 com.JavaScriptCom/com.JavaScriptCom.JavaScriptCom (server)' ~ Channel is unrecoverably broken and will be disposed!
    这个错误,必须用2.1的模拟器,这个估计是BUG.

    附代码:
    AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.JavaScriptCom" android:versionCode="1"
        android:versionName="1.0">
        <application android:icon="@drawable/icon" android:label="@string/app_name">
            <activity android:name=".JavaScriptCom" android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
        <uses-sdk android:minSdkVersion="7" />
    </manifest> 

    JavaScriptCom.java

    package com.JavaScriptCom;
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    public class JavaScriptCom extends Activity {
        final class runJavaScript {
            public void runOnAndroidJavaScript(final String str) {
                h.post(new Runnable() {
                    // @Override
                    public void run() { 
                        TextView show = (TextView) findViewById(R.id.show);
                        show.setText("This is a message from javascript:" + str);
                    }

                });
            }
        }

        private EditText txt;
        private WebView wv;
        private Button btn;
        private Handler h = new Handler();
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            setContentView(R.layout.main);
            txt = (EditText) findViewById(R.id.txt);
            wv = (WebView) findViewById(R.id.wv);
            btn = (Button) findViewById(R.id.btn);
            WebSettings webSettings = wv.getSettings();
            webSettings.setJavaScriptEnabled(true);
            webSettings.setSaveFormData(false);
            webSettings.setSavePassword(false);
            webSettings.setSupportZoom(false);
            wv.addJavascriptInterface(new runJavaScript(), "myjs");
            String url = "file:///android_asset/android.html";
            wv.loadUrl(url);
            btn.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    wv.loadUrl("javascript:get4Android('"
                            + txt.getText().toString() + "')");
                }
            });
        }
    }

    android.html

    <!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd ">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script language="javascript" type="text/javascript">
    function get4Android(str){
     document.getElementById("show").innerHTML="This is a message from android:"+str;
     }
    function send2Android(){
     var str = document.getElementById("mess").value;
     window.myjs.runOnAndroidJavaScript(str);//调用android的函数
     }
    </script>
    </head>
    <body>
     <input type="text" id="mess" />
     <input type="button" value="Send To Android"  onclick="send2Android()"/>
    <div id="show"></div>
    </body>
    </html>

    main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <EditText android:id="@+id/txt" android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <Button android:id="@+id/btn" android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:text="Send To JavaScript" />
        <TextView android:id="@+id/show" android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        

  • 相关阅读:
    【转】wpa_supplicant与wpa_cli之间通信过程
    CSS Hack
    HTML5测试(二)
    HTML5测试(一)
    百分号编码(URL编码)
    DOM事件处理函数
    JS数组
    JS中for循环嵌套
    Codecombat 游戏攻略(计算机科学三)2
    Codecombat 游戏攻略(计算机科学三)
  • 原文地址:https://www.cnblogs.com/sode/p/2618726.html
Copyright © 2020-2023  润新知