电竞比分网-中国电竞赛事及体育赛事平台

分享

Make a Double Click work in a WebView

 quasiceo 2015-04-25

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?

asked May 2 '12 at 17:28

    
Its a really bad idea to ignore the UI conventions for a platform. The double tap gesture to zoom in is well known and used by users, if you start using it to do different things you're only going to make users angry. –  slayton May 2 '12 at 22:50
    
Well I am doing it only for a specific web site that a large companies uses for its employees and needs the double click functionality. I am not trying to replace the web browser for the android. I am simply trying to make a small app for a very specific purpose. –  user1366422 May 3 '12 at 18:22

1 Answer

Try it:

private GestureDetector gs = null;

private OnTouchListener onTouch = new OnTouchListener() {

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

        if (gs == null) {
            gs = new GestureDetector(self,
                    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?

answered Mar 31 '14 at 7:58
yuchi_1k91
11417

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多