2010-11-09 51 views
0

这是我的代码。我很困惑onTouch(),请看看

当返回false时,只能调用ACTION_DOWN,并且返回true时,所有操作都可以,为什么?

aView = findViewById(R.id.a1); 
aView.setOnTouchListener(new OnTouchListener(){ 

    //@Override 
    public boolean onTouch(View v, MotionEvent event) { 

     // TODO Auto-generated method stub 
     System.out.println("Gallery onTouch"); 

     if(event.getAction()==MotionEvent.ACTION_MOVE){ 
      Log.e("touchtest", "ACTION_MOVE"); 
      System.out.println("ACTION_MOVE ");   

     }else if(event.getAction()==MotionEvent.ACTION_UP){ 
      Log.e("touchtest", "ACTION_UP"); 
      System.out.println("ACTION_UP "); 

     }else if (event.getAction()==MotionEvent.ACTION_DOWN){ 
      Log.e("touchtest", "ACTION_DOWN"); 
     } 

     return true; 
    } 

}); 

回答

2

在这种情况下,它总是建议使用开关的情况下,你应该以阻止其他操作,

switch(event.getAction()){ 
     case MotionEvent.ACTION_MOVE: Log.e("touchtest", "ACTION_MOVE"); 
      return true; 
     case MotionEvent.ACTION_UP: Log.e("touchtest", "ACTION_UP"); 
      return true; 
     case MotionEvent.ACTION_DOWN: Log.e("touchtest", "ACTION_DOWN"); 
      return true; 
     default: 
      return false; 
} 

希望它会工作

+0

返回true,这没关系,现在...为什么会出现必须返回true – Nick 2010-11-09 06:20:12

+0

我现在明白了...谢谢 – Nick 2010-11-09 06:22:07