简体   繁体   English

JavaScript onclick在浏览器中有效,在Android WebView中不适用

[英]JavaScript onclick works in browsers, not in Android WebView

I have a simple web application, which I'm trying to display through an Android WebView. 我有一个简单的Web应用程序,试图通过Android WebView进行显示。 The webpage consist of not much more than just one HTML table. 该网页仅包含一个HTML表。 Each of the cells of said table have an onclick property set to a javascript function, which brings up a box with some information. 所述表格的每个单元格都有一个设置为javascript函数的onclick属性,该属性会弹出一个包含一些信息的框。

This works well in all browsers, both desktop and mobile. 这在所有浏览器(台式机和移动设备)上均能很好地工作。 However, when I try to click any of the table cells through my Android WebView, nothing happens, the webpage doesn't seem to recognise the clicks. 但是,当我尝试通过Android WebView单击任何表单元格时,什么也没有发生,该网页似乎无法识别这些单击。

(Note: I did enable JavaScript for the webview, it does exactly what it should, for example when the document is loaded, only it doesn't recognise the clicks and doesn't fire the corresponding events.) (注意:我确实为webview启用了JavaScript,它确实执行了应有的功能,例如,在加载文档时,只是它无法识别点击,并且不会触发相应的事件。)

I've tried searching for an answer, but I had no luck. 我试图寻找答案,但是我没有运气。 All threads I found were about solving different problems. 我发现的所有线程都与解决不同的问题有关。

I'm not trying to run Android code by calling a JavaScript function, I only need the website JS to recognize the clicks. 我并不是想通过调用JavaScript函数来运行Android代码,我只需要网站JS即可识别点击。

What do I have to do to make this work? 我必须做些什么才能使这项工作? Thank you very much in advance, and please don't get too upset if my terminology isn't spot on, I'm just a self-taught enthusiast. 在此先非常感谢您,如果我的用语不正确,请不要感到沮丧,我只是一个自学成才的爱好者。

Try it 试试吧

在此处输入图片说明

Then in the HTML, call it directly from the button tag: 然后在HTML中,直接从button标签调用它:

在此处输入图片说明

Turns out the problem was that I was using setOnTouchListener to handle the WebView clicks and even though I made sure to return false in order to let Android know that the click should be handled further by the WebView, that didn't happen. 原来的问题是我正在使用setOnTouchListener来处理WebView单击,即使我确保返回false也是为了让Android知道应该由WebView进一步处理该单击,但这并未发生。 I believe that could be fixed by calling super.OnTouchEvent(event) somewhere in the code, but I'm not sure how exactly I'd do that and also I found quite a simple approach that in my opinion makes it way clearer what's actually happening. 我相信可以通过在代码中的某个地方调用super.OnTouchEvent(event)来解决此问题,但是我不确定这样做的确切程度,而且我发现了一种非常简单的方法,据我看来,该方法可以更清楚地了解实际内容发生。

The idea is to create a custom "MyWebView" class by extending WebView and override the onTouchEvent method: 这个想法是通过扩展WebView并覆盖onTouchEvent方法来创建自定义“ MyWebView”类:

public class MyWebView extends WebView
{
    private GestureDetector mDetector;

    public MyWebView(Context context)
    {
        super(context);
        mDetector = new GestureDetector(context, new MyGestureListener());
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        mDetector.onTouchEvent(event);
        this.performClick();
        return super.onTouchEvent(event);
    }

    @Override
    public boolean performClick()
    {
        return super.performClick();
    }

    class MyGestureListener extends GestureDetector.SimpleOnGestureListener
    {
        @Override public boolean onDown(MotionEvent event)
        {
            return true;
        }

        @Override public boolean onSingleTapConfirmed(MotionEvent e)
        {
            //do stuff here
            return false;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android WebView JavaScript onClick错误 - Android WebView JavaScript onClick Bug 网站无法在android webview中加载,但可以在android浏览器中正常运行 - Website won't load in android webview but works fine in android browsers 我可以在Android的Webview中使用onclick Javascript来确定X和Y吗? - Can I use onclick Javascript in Webview in Android to determine X and Y? 如果从webviewview调用else语句不能在javascript onclick()中工作? - If else statement not working in javascript onclick() when called from android webview? 如何更改onclick以便在手机浏览器上运行? - How to change onclick so it works on phone browsers? Android WebView-在浏览器中有效,但在WebView中无效 - Android WebView - Works in browser but not WebView Android WebView loadUrl JavaScript无法使用API​​ &lt;= 18(但可以在API&gt; = 19中使用) - Android WebView loadUrl JavaScript not working API <=18 (but works in API >=19) 如何检测Android浏览器而不检测Android App(WebView)? - How to Detect Android Browsers but Not Android App (WebView)? 我在 Android WebView 中收到“Uncaught ReferenceError: globalThis is not defined”HTML5-qrcode 扫描器,在 chrome 和 Web 浏览器上运行完美 - I am getting "Uncaught ReferenceError: globalThis is not defined" HTML5-qrcode scanner in Android WebView, Works perfect on chrome and Web Browsers Android WebView div OnClick在Android 4.4中不起作用 - Android webview div OnClick not working in Android 4.4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM