• Android WebView与网页JS相互调用


     

    在Android中的写法:

    public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.main);

            final WebView web1=(WebView)findViewById(R.id.web1);

            final Button mybutton=(Button)findViewById(R.id.myButton);

            Handler h = new Handler();//声明一个传值的参数

            web1.getSettings().setJavaScriptEnabled(true);//确定这个webview可以用JS

            web1.addJavascriptInterface(new A(), "to2");//将本类对象添加到JS的全局对象window中.

            web1.loadUrl("file:///android_asset/test.html");//这个是加载本工程的HTML地址,放在assets文件夹中

            WebViewClient webclient=new WebViewClient(){

     

               @Override

               public void onPageFinished(WebView view, String url) {

                  // TODO Auto-generated method stub

                  super.onPageFinished(view, url);

               }

            };

            web1.setWebViewClient(webclient);

        }

        

         class A{

        public void a(){

                   这里做相应的逻辑操作。

       }

    在html当中的写法

    <title>无标题文档</title>

    <script type="text/javascript">

    function f2(){

           return window.to2.a();

    }

    </script>

    </head>

    <body>

    <table align="center" width="724px" height="600px">

    <tr align="center">

    <td align="center">

    <input name="submit" type="submit" value="跳转"  onclick="return f2()" />

    </td>

    </tr>

    </table>

    </body>

    </html>

    注明:QQ技术交流群:108614806   感兴趣的加一下。

  • 相关阅读:
    2010年8月26日周四_understanding ArcGIS Server_overview_8.1
    2010年8月23日周一_FeatureLayer_6.13
    2010年8月22日周日_StylingAndSkinningScaleBar_6.12
    2010年8月26日周四_underStanding Flex_FlexComponets_7.4
    Flex组件的项目渲染器(ItemRenderer)使用总结
    2010年8月26日周四_understanding Flex_componentsLifeCycle_7.5
    2010年8月26日周四_understanding Flex_MXML and ActionScript_7.2
    缺页中断
    后缀式
    lua continue
  • 原文地址:https://www.cnblogs.com/zhiqiangsimida/p/2774692.html
Copyright © 2020-2023  润新知