Make a Double Click work in a WebView

微笑、不失礼 提交于 2019-12-11 07:31:48

问题


I have a Website that has some double click functionality and so with android when you double click in the browser it will just zoom. So I am building a WebView so I can over ride the double click and make it react like it should.

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

So the function onDoubleTapEvent(MotionEvent e) will do whatever is in between in the event of a double click. The above code is mostly thanks too Cehm.

So at this point I need to trick it to executing a a double click. Can I just simulate a Couple of click in there?


回答1:


Try it once :

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

Finally, set OnTouch event to WebView:

webView.setOnTouchListener(onTouch);

See detail: How to make Double Click in WebView?



来源:https://stackoverflow.com/questions/10418757/make-a-double-click-work-in-a-webview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!