0

我附加了一个带有 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 以便每次触摸都会触发一次?

4

2 回答 2

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;
                 }
            }
         });
于 2013-04-19T10:05:26.750 回答
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;
}
于 2013-04-19T09:52:38.323 回答