Android屏幕按钮的ButtonDown和ButtonUp事件?

Bre*_*ret 12 android

有没有办法为软按钮获取onButtonDown或onButtonUp事件(即不是物理硬件按钮,而是屏幕上的按钮)?我在屏幕上有一个按钮,我希望用户按住X秒.为此,我需要分别捕获buttonDown和buttonUp事件.

谢谢,

布雷特

dra*_*ard 42

yourButton.setOnTouchListener( yourListener );

public boolean onTouch( View yourButton , MotionEvent theMotion ) {
    switch ( theMotion.getAction() ) {
    case MotionEvent.ACTION_DOWN: break;
    case MotionEvent.ACTION_UP: break;
    }
    return true;
}
Run Code Online (Sandbox Code Playgroud)

  • 最好还检查 MotionEvent.ACTION_CANCEL 是否为“向上”,因为您可能会滑出按钮而错过“向上”操作。 (2认同)