[英]Detect button long press and press android button
I have a button, and i am trying to fire two actions in one button. 我有一个按钮,并且我试图在一个按钮中触发两个动作。 to call the method
takeVideo
on button long press. 长按按钮即可调用方法
takeVideo
。 and on button press i want to call the method imageCapture
. 然后按一下按钮,我想调用
imageCapture
方法。
the below code is working for long press. 下面的代码可以长时间按下。 but i am not able to detect the button press to call only the
imageCapture
method. 但我无法检测到按下按钮仅调用
imageCapture
方法。
takePhotoBtn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
long down;
int action = motionEvent.getAction();
if (action == MotionEvent.ACTION_BUTTON_PRESS) {
imageCapture();
return true;
}
if (action == MotionEvent.ACTION_DOWN) {
takeVideo();
timer.start();
return true;
} else if (action == MotionEvent.ACTION_UP) {
takeVideo();
timer.cancel();
return true;
}
return false;
}
});
EDIT 编辑
i am not using OnLongClickListener
and OnClickListener
. 我没有使用
OnLongClickListener
和OnClickListener
。
i want to perform other actions on MotionEvent.ACTION_UP
so i am trying to solve my problem using setOnTouchListener
in this case 我想执行的其他动作
MotionEvent.ACTION_UP
所以我试图用解决我的问题setOnTouchListener
在这种情况下,
You can get the duration of the Long Press over official documentation ( getLongPressTimeOut
) It use to be 1000ms and now 500ms... it could change. 您可以通过官方文档获取长按的持续时间(
getLongPressTimeOut
),以前是1000毫秒,现在是500毫秒...可以更改。 That's why you need to get the timeout. 这就是为什么您需要超时。
Then, over ACTION_DOWN you can count the ms... if ms >= 500
(if currently is 500ms) then is a LongPress. 然后,在ACTION_DOWN上,您可以计算ms ...如果
ms >= 500
(如果当前为500ms),则为LongPress。 But, like Marios says, use the OnLongClickListener and OnClickListener for that. 但是,就像Marios所说的那样,使用OnLongClickListener和OnClickListener 。
Why don't you use 你为什么不使用
onLongClickListener ( http://developer.android.com/reference/android/view/View.OnLongClickListener.html ) onLongClickListener( http://developer.android.com/reference/android/view/View.OnLongClickListener.html )
and 和
OnClickListener ( http://developer.android.com/reference/android/view/View.OnClickListener.html ) OnClickListener( http://developer.android.com/reference/android/view/View.OnClickListener.html )
setOnLongClickListener
can be used for this. setOnLongClickListener
可以用于此目的。
takePhotoBtn.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return true;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.