qq_31818981 2016-07-15 05:45 采纳率: 0%
浏览 2873
已结题

Android:如何监听webView中弹出的软键盘????急求!!

Android:如何监听webView中弹出的软键盘????急求!!

  • 写回答

1条回答 默认 最新

  • Moluth 2016-07-15 08:51
    关注

    使用js监听,然后,调用安卓中的方法,将值传给安卓即可

    public class MainActivity extends Activity {
    
        WebView wv;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            wv=(WebView) findViewById(R.id.wv);
            wv.setWebViewClient(new WebViewClient());
            initData();
        }
        private void initData() {
            wv=(WebView) findViewById(R.id.wv);
            wv.setWebViewClient(new WebViewClient());//防止跳转到手机浏览器
            WebSettings settings = wv.getSettings();
            settings.setJavaScriptEnabled(true);////启用javascript
             //指定可供js调用的java对象
             wv.addJavascriptInterface(new A(), "bridge");
             wv.loadUrl("file:///android_asset/a.html");
        }
    
        public class A
        {
            @JavascriptInterface
            public void show(String s)
            {
                Toast.makeText(MainActivity.this, s, 0).show();
            }
        }
    
    } 
    

    下面是a.html

    
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>无标题文档</title>
    <script type="text/javascript">
    function aa()
    {
        var w=document.getElementById("hhh");
        w.style.backgroundColor="#ff0000";
    }
    function show()
    {
        w=document.getElementById("input");
        bridge.show(w.value);
    }
    </script>
    </head>
    
    <body>
        <div style="background-color:#335577; width:30px; height:50px" id="hhh">
    
        </div>
        <input type="text" id="input" />
        <button onClick="aa();">哈哈</button><br/>
        <button onClick="show();">哈哈</button>
    </body>
    </html>
    
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝