1

我有一个网站,它有一些双击功能,所以当你在浏览器中双击它时,它只会缩放。所以我正在构建一个 WebView,这样我就可以跳过双击并让它做出应有的反应。

public class myWebView extends Activity{

GestureDetector gs = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    WebView engine = (WebView) findViewById(R.id.web_engine);
    engine.getSettings().setJavaScriptEnabled(true);
    engine.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    engine.getSettings().setSupportMultipleWindows(true);
    engine.setWebChromeClient(new WebChromeClient() {
        @Override
        public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                    return super.onJsAlert(view, url, message, result);
           }
    });
    engine.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }
            });

    engine.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (gs == null) {
                gs = new GestureDetector(
                        new GestureDetector.SimpleOnGestureListener() {
                            //overrides the double click
                            @Override
                            public boolean onDoubleTapEvent(MotionEvent e) {
                                // SOME CODE THAT SIMULATES A DOUBLE CLICK
                                return true;
                            }
                        });
            }
            gs.onTouchEvent(event);
            return false;
        }
    }); 
    engine.loadUrl("http://www.google.com");
}
}

因此,函数 onDoubleTapEvent(MotionEvent e) 将在双击事件中执行介于两者之间的任何操作。 上面的代码也主要是感谢 Cehm

所以此时我需要欺骗它执行双击。我可以在那里模拟几次点击吗?

4

1 回答 1

2

尝试一次:

private GestureDetector gs = null;

private OnTouchListener onTouch = new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        if (gs == null) {
            gs = new GestureDetector(
                    new GestureDetector.SimpleOnGestureListener() {
                        @Override
                        public boolean onDoubleTapEvent(MotionEvent e) {

                            //Double Tap
                            snapWebView.zoomIn();//Zoom in
                            return true;
                        }

                        @Override
                        public boolean onSingleTapConfirmed(MotionEvent e) {

                            //Single Tab
                            snapWebView.zoomOut();// Zoom out
                            return false;
                        };
                    });
        }

        gs.onTouchEvent(event);

        return false;
    }
};

最后,将 OnTouch 事件设置为 WebView:

webView.setOnTouchListener(onTouch);

查看详细信息:如何在 WebView 中进行双击?

于 2014-03-31T07:58:54.077 回答