Android屏幕按钮的按钮向下和按钮向上事件?

时间:2010-05-01 17:11:57

标签: android

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

谢谢,

布雷特

2 个答案:

答案 0 :(得分:41)

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;
}

答案 1 :(得分:0)

Kotlin变体

button.setOnTouchListener(object : View.OnTouchListener {
        override fun onTouch(v: View?, event: MotionEvent?): Boolean {
            when (event?.action) {
                MotionEvent.ACTION_DOWN -> {   }
                MotionEvent.ACTION_UP   -> {   }
            }
            return v?.onTouchEvent(event) ?: true
        }
    })