在WebView中进行双击工作

时间:2012-05-02 17:28:40

标签: android webview override double-click simulate

我有一个网站有一些双击功能,因此当你在浏览器中双击它时,它只会缩放。所以我正在构建一个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)函数将在双击的情况下执行介于两者之间的任何操作。 The above code is mostly thanks too Cehm

所以在这一点上我需要欺骗它来执行双击。我可以在那里模拟一下点击吗?

1 个答案:

答案 0 :(得分: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);

详见:How to make Double Click in WebView?