Android Touch EventListener触发回调不止一次



我用一个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,以便我将被触发一次每次触摸?

 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;
                 }
            }
         });

它是由多个动作触发的因为触摸事件有很多像down, up和move等事件时间所以你需要用down time或up time等特定事件来实现它

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;
}

最新更新