2011-05-26 202 views
4

我的android應用程序中的觸摸監聽器有問題。 OnTouchLIstenr不適用於視圖,即ACTION_DOWN在偵聽器中表現良好,但ACTION_UP不會調用。我不知道最近發生了什麼問題。但是,如果我設置了虛擬點擊偵聽器,兩者都可以正常工作。爲什麼這樣?觸摸監聽器不工作android

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    ImageView image = (ImageView) findViewById(R.id.image); 
    image.setOnTouchListener(new OnTouchListener() {    
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      ImageView img = (ImageView) v; 
      int action = event.getAction(); 
      if (action == MotionEvent.ACTION_DOWN){ 
       img.setImageResource(R.drawable.port); 
      }else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL){ 
       img.setImageResource(R.drawable.bar); 
      }    
      return false; 
     } 
    }); 

} 

回答

3
image.setOnTouchListener(new OnTouchListener() {    
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      ImageView img = (ImageView) v; 
      int action = event.getAction(); 
      if (action == MotionEvent.ACTION_DOWN){ 
       img.setImageResource(R.drawable.port); 
      }else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL){ 
       img.setImageResource(R.drawable.bar); 
      }    


     return true; 
     } 
    }); 

只需更換return true;代替return false;和檢查