Android的:如何知道按钮不放 [英] Android: how to know if button is held down
问题描述
我玩Android上的游戏,我有一个函数MoveCharacter(INT方向)时,按钮是pressed
I am playing with a game on Android, and I have a function MoveCharacter (int direction) that moves an animated sprite when buttons are pressed
例如,当用户presses了,我有这个code:
For example, when user presses up I have this code:
mControls.UpButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mLevel.mCharAnimation.FrameUp();
}
});
不过,我希望能够保持,只要用户保持按钮向下移动的字符。
出人意料的是,我还没有找到如何在Android中做到这一点。
是否有某种onButtonDownLister的?
However, I’d like to be able to keep moving the character as long as the user keeps the button down. Surprisingly, I have not found out how to do this in Android. Is there some kind of onButtonDownLister?
推荐答案
您需要使用<一个href=\"http://developer.android.com/reference/android/view/View.OnTouchListener.html\">OnTouchListener有对倒行为分开,向上和其他国家。
You need to use an OnTouchListener to have separate actions for down, up, and other states.
mControls.UpButton.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Do something
return true;
case MotionEvent.ACTION_UP:
// No longer down
return true;
}
return false;
}
});
这篇关于Android的:如何知道按钮不放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!