Android Touch EventListener不止一次触发回调

时间:2013-04-19 09:49:28

标签: android event-handling ontouchevent

我附加了一个带有ImageView对象的触摸事件监听器,

imageview_obj.setOnTouchListener(new View.OnTouchListener()
{
    public boolean onTouch(View v, MotionEvent event) 
    {
            Log.i(" info "," message");                        
            return true;
        }
     });

问题是当我触摸imageview_obj时,回调被多次触发(3次到4次)..

现在我的问题是

  1. 为什么回调会被多次触发?

  2. 我应该如何附加onTouchListener,以便每次触摸都会触发一次?

2 个答案:

答案 0 :(得分:1)

 imageview_obj.setOnTouchListener(new View.OnTouchListener()
    {
        public boolean onTouch(View v, MotionEvent event) 
        {
                Log.i(" info "," message");                        
                return true;
                  switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
//Do code here for down
               return true;
            case MotionEvent.ACTION_MOVE:
            return true;
            case MotionEvent.ACTION_UP:
//Do code here for up
            return true;
                 }
            }
         });

答案 1 :(得分:0)

它是在多个动作上触发的,因为触摸事件有许多像向下,向上和移动等事件时间,因此您需要使用特定事件(如停机时间或正常运行时间)实现此操作

public boolean onTouch(View v, MotionEvent event) 
{

    int action = event.getAction();
    switch(action){

        case Down_Action: // MotionEvent class field 
        Log.i(" info "," message");      
        break;
        case Up_Action:
        Log.i(" info "," message");      
        break;
        case Move_action:
        Log.i(" info "," message");      
        break;

    }

    return true;
}