Android的枪王Android

2023-09-12 04:36:30 作者:伱说"你依然是我喜欢的人

我如何在活动中使用双击事件?双击事件或长按方式工作(尽管该方法得到重写),我只是把举杯消息中每一种方法,但没有结果!你能帮忙吗?

How do I use double tap events in an activity? The double tap event or long click method working (though the method is getting overriden) I've just put in a toast message in each of these methods and yet no result! Can you help?

推荐答案

做双击最简单的方法是用一个GestureDetector检测到它。该绝招是,以确保您委派活动的的onTouchEvent到GestureDetector的的onTouchEvent:

The easiest way to do a double-tap is to detect it with a GestureDetector. The "trick" is to be sure you delegate the Activity's onTouchEvent to the GestureDetector's onTouchEvent:

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.Toast;

public class MainActivity extends Activity {

    private GestureDetector gestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onDoubleTap(MotionEvent e) {
                Toast.makeText(MainActivity.this, "double tap", Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (gestureDetector.onTouchEvent(event))
            return true;
        return super.onTouchEvent(event);
    }
}