Android Listener来检测何时按下屏幕 [英] Android Listener to detect when the screen is being pressed
问题描述
我的Android应用程序中有一个视图,当按下屏幕时该视图应不断更新.我发现,大多数收听者只会在用户首次触摸屏幕或用户将手从屏幕上移开时使视图无效一次.
I have a view in my Android app that should be updated constantly when the screen is being pressed. Most listeners I found only invalidate the view once when the user touches the screen for the first time, or when the user removes his hand from the screen.
是否存在只要触摸屏幕就不断触发的监听器之类的问题?还是有其他方法可以做到这一点?我知道,这听起来确实很简单,但是我还没有找到一种方法.
Is there such a thing as a listener that is constantly triggered as long as the screen is touched ? Or is there any other way to do this ? I know, that sounds like something really simple, but I haven't found a way to do it.
推荐答案
覆盖onTouch()
并在ACTION_DOWN
中设置标志.只要此后未调用ACTION_UP
,用户就在触摸屏幕.
Override onTouch()
and set a flag in ACTION_DOWN
. As long as ACTION_UP
isn't called after that, the user is touching the screen.
boolean pressed = false;
@Override
public boolean onTouch(View v, MotionEvent event) {
//int action = event.getAction();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
pressed = true;
break;
case MotionEvent.ACTION_MOVE:
//User is moving around on the screen
break;
case MotionEvent.ACTION_UP:
pressed = false;
break;
}
return pressed;
}
这篇关于Android Listener来检测何时按下屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!