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

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

问题描述

我对 javascript 不是很熟悉,但我认为这是实现我的目的的最佳方式.如果没有,请纠正我.

我最后有一个许可证文本 2 个按钮.所有这些都是在 WebView 中用 HTML 编写的,因为许可证中有一些链接.现在,我希望当用户单击 WebView 中的确定"按钮时,这会触发一些 javascript 或侦听器,我可以在 Java 中抓取它们以触发 Intent 去在应用程序中前进.(取消按钮会做相反的事情,但如果我知道如何做一个,我可以做另一个.;))

这是否会给某人敲响警钟?欢迎任何解释或示例代码.

解决方案

在阅读了一些文章后,我终于自己解决了.如果您对 javascript 一无所知,并且文档对这个主题非常薄,这有点困难.
这是我的解决方案,希望这会对其他人有所帮助:

一个包含 2 个按钮的 HTML 页面,如下所示:

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

我将点击事件发送到 HTML 页面顶部的 javascript :

validrefuse 是我通过 javascript 接口传递以使用它们的方法的 2 个 java 对象.因此,在 Java 中,我创建了 2 个按钮(并未真正显示在 Activity 中,仅在此处显示它们的方法,并且是 HTML 按钮的阴影:

valid = new Button(ctx);valid.setOnClickListener(this);拒绝 = 新按钮(ctx);拒绝.setOnClickListener(this);

然后我将 javascript 添加到我的 WebView 中:

//启用javascriptWebSettings ws = wv.getSettings();ws.setJavaScriptEnabled(true);//添加记录javascript事件的接口wv.addJavascriptInterface(valid, "valid");wv.addJavascriptInterface(拒绝,拒绝");

最后,在 onClick 方法中处理点击事件:

@Overridepublic void onClick(View v) {如果(v.equals(有效)){//做一点事} else if (v.equals(refuse)) {//做别的事 }}

希望对大家有所帮助

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

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. ;) )

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

解决方案

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 :

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>

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>

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);

Then I added javascript to my WebView :

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

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

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

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