Android屏幕上的双击事件

时间:2013-06-10 07:07:56

标签: android toast double-click

我是Android新手。实际上每当我双击Android屏幕(布局)时,我想创建一个Toast。但是每当我双击屏幕时都没有任何反应。请解决我的问题。

public class MainActivity extends Activity implements OnDoubleTapListener,OnGestureListener{

   int clickCount = 0;
   long startTime;
   long duration;
   static final int MAX_DURATION = 500;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    GestureDetector detector = new GestureDetector(this,this);
    detector.setOnDoubleTapListener(new OnDoubleTapListener() {

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            if(e.getAction()==1)
            {
            Toast.makeText(getBaseContext(), "onSingleTapConfirmed", Toast.LENGTH_LONG).show();
            }
        return true;
        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {
            if(e.getAction()==1)
            {
            Toast.makeText(getBaseContext(), "onDoubleTapEvent", Toast.LENGTH_LONG).show();
            }
            return true;
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            if(e.getAction()==1)
            {
            Toast.makeText(getBaseContext(), "onDoubleTap", Toast.LENGTH_LONG).show();
            }
            return true;
        }
    });

}

3 个答案:

答案 0 :(得分:2)

在您的活动中:

@Override
public boolean onTouchEvent(MotionEvent event) {
    return detector.onTouchEvent(event);
}

如果要在特定视图上附加探测器

LinearLayout layout = (LinearLayout) findViewById(R.id.yourLinearLayoutId);
layout.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {      
                return detector.onTouchEvent(event);
            }
});

答案 1 :(得分:0)

试试这个

   LinearLayout layout = (LinearLayout) findViewById(R.id.yourLinearLayoutId);
   layout .setOnTouchListener(this);
   gestureDetector = new GestureDetector(new MyGestureDetector());

并像内部类

一样使用此类
 public class MyGestureDetector extends SimpleOnGestureListener implements OnGestureListener   {

           @Override
           public boolean onDoubleTap(MotionEvent e) {
             Toast.makeText(getBaseContext(), "onDoubleTapEvent", Toast.LENGTH_LONG).show();

                return super.onDoubleTap(e);

                }
            }


@Override
public boolean onTouch(View v, MotionEvent event) {
    if (gestureDetector.onTouchEvent(event)){
        Log.v(TAG,"screen touched");
        return true;
    }
    else{
        return false;
    }
}

答案 2 :(得分:0)

如果您正在寻找双击等内容,则必须使用GestureDetector.OnDoubleTapListener