有没有办法为软按钮获取onButtonDown或onButtonUp事件(即不是物理硬件按钮,而是屏幕上的按钮)?我在屏幕上有一个按钮,我希望用户按住X秒。为此,我需要分别捕获buttonDown和buttonUp事件。
谢谢,
布雷特
答案 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
}
})