Android onTouch with onClick and onLongClick

你说的曾经没有我的故事 提交于 2019-12-01 00:58:38

问题


I've got a custom view which acts like a button. I want to change the background when user press it, revert the background to original when user moves the finger outside or release it and I also want to handle onClick/onLongClick events. The problem is that onTouch requires me to return true for ACTION_DOWN or it won't send me the ACTION_UP event. But if I return true the onClick listener won't work.

I thought I solved it by returning false in onTouch and registering onClick - it somehow worked, but was kinda against the docs. I've just received a message from an user telling me that he's not able to long-click on the button, so I'm wondering what's wrong here.

Part of the current code:

public boolean onTouch(View v, MotionEvent evt)
{
  switch (evt.getAction())
  {
    case MotionEvent.ACTION_DOWN:
    {
      setSelection(true); // it just change the background
      break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_OUTSIDE:
    {
      setSelection(false); // it just change the background
      break;
    }
  }

  return false;
}

public void onClick(View v)
{
  // some other code here
}

public boolean onLongClick(View view)
  {
    // just showing a Toast here
    return false;
  }


// somewhere else in code
setOnTouchListener(this);
setOnClickListener(this);
setOnLongClickListener(this);

How do I make them work together correctly?

Thanks in advance


回答1:


onClick & onLongClick is actually dispatched from View.onTouchEvent.

if you override View.onTouchEvent or set some specific View.OnTouchListener via setOnTouchListener, you must care for that.

so your code should be something like:

public boolean onTouch(View v, MotionEvent evt)
{
  // to dispatch click / long click event,
  // you must pass the event to it's default callback View.onTouchEvent
  boolean defaultResult = v.onTouchEvent(evt);

  switch (evt.getAction())
  {
    case MotionEvent.ACTION_DOWN:
    {
      setSelection(true); // just changing the background
      break;
    }
    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_OUTSIDE:
    {
      setSelection(false); // just changing the background
      break;
    }
    default:
      return defaultResult;
  }

  // if you reach here, you have consumed the event
  return true;
}


来源:https://stackoverflow.com/questions/7387572/android-ontouch-with-onclick-and-onlongclick

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