Android 长按安卓
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11701826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Long press Android
提问by Nikola Ninkovic
I have problems with detecting long press in my custom view.
我在自定义视图中检测长按时遇到问题。
Here's the code related to this issue
这是与此问题相关的代码
final GestureDetector gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
public void onLongPress(MotionEvent e) {
Log.e("dbg_msg", "onLongPress");
}
});
public boolean onTouchEvent(MotionEvent event) {
return gestureDetector.onTouchEvent(event);
};
This code detects every single (short) click as long press.
此代码将每次(短)点击检测为长按。
When I put this code in class inherited from Activity, it works.
当我将此代码放在从 Activity 继承的类中时,它会起作用。
So why it isn't working in custom View ?
那么为什么它在自定义 View 中不起作用?
回答by Nikola Ninkovic
All of this code goes in your custom view class:
所有这些代码都在您的自定义视图类中:
public static int LONG_PRESS_TIME = 500; // Time in miliseconds
final Handler _handler = new Handler();
Runnable _longPressed = new Runnable() {
public void run() {
Log.i("info","LongPress");
}
};
@Override
public boolean onTouchEvent(MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
_handler.postDelayed(_longPressed, LONG_PRESS_TIME);
break;
case MotionEvent.ACTION_MOVE:
_handler.removeCallbacks(_longPressed);
break;
case MotionEvent.ACTION_UP:
_handler.removeCallbacks(_longPressed);
break;
}
return true;
}
回答by Anh3Saigon
I'm not sure but your GestureDetector
's constructor is deprecated (here). Could you try other ones which need a context as first parameter?
我不确定,但您GestureDetector
的构造函数已被弃用(此处)。你能试试其他需要上下文作为第一个参数的吗?
Sorry I'm new so I can't post comment.
对不起,我是新手,所以我不能发表评论。
— Edited—
— 已编辑—
It seems you used another listener, this View.OnTouchListener
has other onTouch()
method. Could you try again?
看来您使用了另一个侦听器,这View.OnTouchListener
有其他onTouch()
方法。你能再试一次吗?
— Edited—
— 已编辑—
Here is an example (worked for me):
这是一个示例(对我有用):
...
mAnotherView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
});
...
mGestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
@Override
public void onLongPress(MotionEvent e) {
// do your tasks here
}
});
回答by MJD
Did you enable long presses on your GestureDetector? You can enable it either using an appropriate constructor, or by calling setIsLongpressEnabled. For instance, you can do:
您是否在 GestureDetector 上启用了长按?您可以使用适当的构造函数或通过调用setIsLongpressEnabled来启用它。例如,您可以执行以下操作:
gestureDetector.setIsLongpressEnabled(true);
in your constructor.
在你的构造函数中。
回答by rjsandim
It's better I guess..
我想更好..
public class Workflow extends View implements View.OnLongClickListener {
public Workflow(Context context, DisplayFeatures d) {
super(context);
setLongClickable(true);
setOnLongClickListener(this);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
/* onTouchEvent should return super.onTouchEvent(event);, otherwise long click wouldn't be performed */
return super.onTouchEvent(event);
}
@Override
public boolean onLongClick(View v) {
Log.d("VIEW", "LONG CLICK PERFORMED!");
return false;
}
}