我想为我的视图实现一个监听器。我想检测一个简单的点击双击和长按,这就是全部。这是我写的代码(仅限相关部分):
public class BoardView extends View implements OnGestureListener, OnDoubleTapListener{
//My functions and code.
@Override
public boolean onTouchEvent(MotionEvent event){
Toast t = Toast.makeText(this.getContext(), "I'm touched", Toast.LENGTH_SHORT);
t.show();
System.err.println("On Touch");
return super.onTouchEvent(event);
}
public void onLongPress(MotionEvent event) {
Toast t = Toast.makeText(this.getContext(), "I get it, stop touching me!!!", Toast.LENGTH_SHORT);
t.show();
System.err.println("Long Press");
//Log.d(DEBUG_TAG, "onLongPress: " + event.toString());
}
@Override
public void onGesture(GestureOverlayView overlay, MotionEvent event) {
}
@Override
public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
}
@Override
public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
}
@Override
public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
// TODO Auto-generated method stub
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
return false;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
Toast t = Toast.makeText(this.getContext(), "Soy double the joy", Toast.LENGTH_SHORT);
t.show();
System.err.println("Double Tap");
return true;
}
}
我完全检测到触摸但不是双击或长按......我做错了什么?
编辑:根据其中一个答案,我写了以下代码:
private class Gestures extends SimpleOnGestureListener{
public Gestures(){
}
public void onTouchEvent(MotionEvent event){
System.err.println("Touch Even gestures");
}
@Override
public void onLongPress(MotionEvent event) {
System.err.println("Long Press");
}
@Override
public boolean onDoubleTap(MotionEvent event) {
System.err.println("Double Tap");
return true;
}
}
Gestures gdetector = new Gestures();
这是在我的BoardView构造函数之前。然后据我所知,我应该做的:
@Override
public boolean onTouchEvent(MotionEvent event){
gdetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
但是我只检测到了简单的触摸......
答案 0 :(得分:0)
不是编写自定义视图,而是查看View.setOnLongClickListener()和View.setOnClickListener()。
http://developer.android.com/reference/android/view/View.html
LongClickListener绝对是您想要长时间点击的内容,而对于某些状态跟踪变量,正常的onClickListener可以处理双击。
答案 1 :(得分:0)
您只需在视图的某个内部类中扩展GestureDetector.SimpleOnGestureListener
并覆盖您想要的方法(双击,长按等),您就可以使用包含此侦听器的手势检测器来处理这些手势。
示例视图可能如下所示:
public class GestureView extends View
{
private GestureDetector mGestureDetector;
//only one constructor for shorter code, in final code all should be presented
public GestureView(Context context, AttributeSet attrs)
{
super(context, attrs);
final MyGestureListener myGestureListener = new MyGestureListener();
mGestureDetector = new GestureDetector(context, myGestureListener);
mGestureDetector.setOnDoubleTapListener(myGestureListener);
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
return mGestureDetector.onTouchEvent(event) || super.onTouchEvent(event);
}
private class MyGestureListener extends GestureDetector.SimpleOnGestureListener
{
@Override
public boolean onDown(MotionEvent event)
{
return true;
}
@Override
public void onLongPress(MotionEvent e)
{
//your code here
}
@Override
public boolean onDoubleTap(MotionEvent e)
{
//your code here return true if event handled
return super.onDoubleTap(e);
}
//this can be taken as click
@Override
public boolean onSingleTapConfirmed(MotionEvent e)
{
//your code here return true if event handled
return super.onSingleTapConfirmed(e);
}
}
}
如果您只想将手势检测器添加到其他视图中,您只需从onTouchListener调用其onTouch方法,如
view.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
return gestureDetector.onTouchEvent(event);
}
});