2011-10-01 27 views
0

我一直在想方設法檢查按鈕和/或圖像是否被按下。但是我需要做不同的事情,具體取決於是否被按下或未按下。這是一個例子。 玩家按住一個火按鈕,角色繼續開火,直到按鈕被擡起。我會如何去做這件事? 感謝您的時間,所以如果我的問題是模糊的。有沒有一些onClickDownListener/onClickUpListener?

回答

3

您可以只設置一個OnTouchListener:

ImageView myView = (ImageView)findViewById(R.id.my_image_view);   
myView.setOnTouchListener(new View.OnTouchListener(){ 
    public boolean onTouch(View v, MotionEvent event){ 
     if (event.getAction() == MotionEvent.ACTION_DOWN){ 
      // do here what ought to happen until the finger is lifted 
     }else if(event.getAction() == MotionEvent.ACTION_UP){ 
      // stop the action because the finger is lifted 
     } 
    } 
}); 

希望這有助於

-serkan