[英]what EVENT LISTENER should i use for my android app
In my app, when one particular image button is clicked and held, i must be able to calculate the time for which the image button was held pressed. 在我的应用程序中,当单击并按住一个特定图像按钮时,我必须能够计算按下图像按钮的时间。 Can any one help me by giving some simple guidance or sample code. 任何人都可以通过提供一些简单的指导或示例代码来帮助我。 i am really stuck up here. 我真的被困在这里了。 Is there any specific event listener for this particular requirement. 是否有针对此特定要求的特定事件侦听器。 I am writing this app specifically for a touch screen phones only. 我正在专门为触摸屏手机编写这个应用程序。
What you want to use is this: 你想用的是:
OnTouchListener touchListener = new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
Log.d(LOG_TAG, "Action is: " + action);
switch (action){
case MotionEvent.ACTION_DOWN:
timeAtDown = SystemClock.elapsedRealtime();
break;
case MotionEvent.ACTION_UP:
long durationOfTouch = SystemClock.elapsedRealtime() - timeAtDown;
Log.d(LOG_TAG, "Touch event lasted " + durationOfTouch + " milliseconds.");
break;
}
return false;
}
};
button.setOnTouchListener(touchListener);
timeAtDown is a long
defined as a class field, since it needs to persist between calls to the touchListener. timeAtDown是一个long
定义为类字段,因为它需要在对touchListener的调用之间保持不变。 Using this you don't interfere with the button's normal operation either; 使用此功能,您也不会干扰按钮的正常操作; you can set a click listener which will function properly. 你可以设置一个能正常运行的点击监听器。 Note: The 'click' operation doesn't happen until the touch event's action goes from DOWN (or MOVE) to UP. 注意:直到触摸事件的动作从DOWN(或MOVE)变为UP时才会发生'click'操作。
Experiment with the on*** callbacks from http://developer.android.com/reference/android/view/KeyEvent.Callback.html . 尝试使用http://developer.android.com/reference/android/view/KeyEvent.Callback.html上的on ***回调。
For example Button.onKeyDown, save the current time in a variable and Button.onKeyDown calculate the difference from last saved time. 例如Button.onKeyDown,将当前时间保存在变量中,Button.onKeyDown计算与上次保存时间的差值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.