通过JavaScript在Android中的WebView检测点击HTML按钮 [英] Detect click on HTML button through javascript in Android WebView

查看:328
本文介绍了通过JavaScript在Android中的WebView检测点击HTML按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是非常熟悉JavaScript,但我认为这是实现我的目标的最佳途径。如果没有,请大家指正。

I'm not highly familiar with javascript but I think this is the best way to accomplish my purpose. If not, please correct me.

我有一个许可证文本在最后2个按钮。所有这一切都写在HTML中的WebView 因为有许可一些链接。现在,我想,当用户单击的WebView确定按钮,这会触发一些JavaScript或听众,我可以在Java中抢火的意图前进中的应用。 (Cancel按钮会做相反的,但如果我知道怎么做的,我可以做其他的。))

I have a licence text 2 buttons at the end. All of this is written in HTML in a WebView because there are some links in the licence. Now, I want that when the user clicks the "ok" button in the WebView, this triggers some javascript or listener that I can grab in Java to fire an Intent to go forward in the application. (The cancel button would do the opposite, but if I know how to do one, I can do the other. ;) )

这是否响钟声任何给别人? 任何解释或样品code是值得欢迎的。

Does this rings any bell to someone? Any explanation or sample code is welcome.

推荐答案

我终于有些看完后明白了我自己。有点困难,当你一无所知的JavaScript当文档是关于这个问题相当薄。
下面是我的解决方案,希望这将有助于其他人:

I finally got it on my own after some reading. Kind of hard when you know nothing about javascript and when the doc is quite thin on the subject.
Here is my solution, hope this will help others :

使用含2个按钮,在这样的年底的HTML页面:

With an HTML page containing 2 buttons at the end like that :

<div>
     <button type="button" id="ok" style="font-weight: 700; margin-right: 20px;" onclick="validClick();">J'accepte</button>
     <button type="button" id="no" onclick="refuseClick();">Je refuse</button>
</div>

我送的点击的JavaScript事件在我的HTML页面的顶部:

I send the event of the click to the javascript at the top of my HTML page :

<script language="javascript">

   function validClick()
   {
      valid.performClick();
      document.getElementById("ok").value = "J'accepte";
   }
   function refuseClick()
   {
      refuse.performClick();
      document.getElementById("no").value = "Je refuse";
   }

</script>

有效拒绝是我穿过的JavaScript接口来使用他们的方法2 Java对象。因此,在Java中,我创建了2个按钮(没有真正显示在活动,只有在这里为他们的方法,并且是在HTML按钮阴影排序:

valid and refuse are 2 java objects that I passed through the javascript interface to use their methods. So in java, I created 2 buttons (not really displayed in the Activity, only here for their methods and are sort of shadows of the HTML buttons :

valid = new Button(ctx);
valid.setOnClickListener(this);
refuse = new Button(ctx);
refuse.setOnClickListener(this);

然后,我添加JavaScript来我的WebView

// Enablejavascript
WebSettings ws = wv.getSettings();
ws.setJavaScriptEnabled(true);
// Add the interface to record javascript events
wv.addJavascriptInterface(valid, "valid");
wv.addJavascriptInterface(refuse, "refuse");

最后,处理在onclick方法的单击事件:

And finally, handle the click events in the onClick method :

@Override
public void onClick(View v) {
    if (v.equals(valid)) {
        //do Something
    } else if (v.equals(refuse)) {
        //do Something else }
}

希望这将帮助一些人

Hope this will help some people

这篇关于通过JavaScript在Android中的WebView检测点击HTML按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆