0

我是安卓新手。实际上,每当我在 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;
        }
    });

}
4

3 回答 3

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);
            }
});
于 2013-06-10T07:13:07.447 回答
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;
    }
}
于 2013-06-10T07:14:42.097 回答
0

如果您正在寻找像 Double Tap 这样的东西,那么您必须使用 GestureDetector.OnDoubleTapListener

于 2013-06-10T07:17:49.407 回答