onLongClickListener does not work on WebView

孤街浪徒 提交于 2020-01-03 17:39:33

问题


I have the following struktur to implement an longclicklistener. It works if I click on a text on the webview which contains a html-link, so I know the structure is not completely wrong.

I removed this link now and the listener just doesn't listen to clicks anymore. Does anybody know this problem and have some advices?

    private View.OnLongClickListener mLongClickHandler = new View.OnLongClickListener()   {
    @Override
    public boolean onLongClick(View view) {
        ...
        return true;
    }
};

...

mywebview.setOnLongClickListener(mLongClickHandler);

回答1:


I tried now to clone the longclick action by myself. This works but only a few times. After a certain time, the onTouch-Event is not called anymore... Suggestions?

private Runnable copyTextAfterDelay=new Runnable() {
    public void run() {
        ...
    }
};

...

        myWebView.setOnTouchListener(new View.OnTouchListener() { 
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) { 
                    case MotionEvent.ACTION_DOWN:  
                        mTimerHandler.removeCallbacks(copyTextAfterDelay);
                        mTimerHandler.postDelayed(copyTextAfterDelay,1000);
                        break;
                    case MotionEvent.ACTION_UP: 
                        mTimerHandler.removeCallbacks(copyTextAfterDelay);
                        break;
                    case MotionEvent.ACTION_MOVE:
                        mTimerHandler.removeCallbacks(copyTextAfterDelay);
                        break;
                }
                return false;                  
            }
            });



回答2:


Override onTouch methode of your webview and return true for ACTION_DOWN events. Thereby you consume your down event.

  @Override
  public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) { 
         case MotionEvent.ACTION_DOWN:  
            return true;
      }
   }


来源:https://stackoverflow.com/questions/16937068/onlongclick-is-not-working-on-webview-os-4-1

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