用户将手指从按钮上移开时如何处理动作?

时间:2018-12-17 18:25:08

标签: android android-layout user-interface button ontouchlistener

当用户通过单击将手指从按钮上拉开但不释放时,我需要处理该动作。我尝试使用OnTouchListener,但据我了解,它仅处理手指的移动或按钮的释放。

2 个答案:

答案 0 :(得分:0)

还存在OnTouchListener

view.setOnTouchListener(new View.OnTouchListener() {
   @Override
   public boolean onTouch(View v, MotionEvent event) {
       if(event.getAction() == MotionEvent.ACTION_MOVE) {
           Rect r = new Rect(0, 0, view.getWidth(), view.getHeight());
           if (!r.contains((int)event.getX(), (int)event.getY()))
               // your action
           return true;
       }

       return false;
   }
});

答案 1 :(得分:0)

@Override
public boolean onTouchEvent(MotionEvent event) {


    int action = event.getActionMasked();

    switch (action) {

        case MotionEvent.ACTION_DOWN:


            Log.d(TAG, "Action was DOWN");
            break;

        case MotionEvent.ACTION_MOVE:
            Log.d(TAG, "Action was MOVE");
            break;

        case MotionEvent.ACTION_UP:


            Log.d(TAG, "Action was UP");



            break;

        case MotionEvent.ACTION_CANCEL:
            Log.d(TAG,"Action was CANCEL");
            break;

        case MotionEvent.ACTION_OUTSIDE:

            Log.d(TAG, "Movement occurred outside bounds of current screen element");
            break;
    }

    return super.onTouchEvent(event);
}

该代码可以帮助您吗?