Android双击

时间:2013-01-24 13:49:26

标签: android android-activity

如何在活动中使用双击事件?双击事件或长按方法工作(虽然方法被覆盖)我只是在每个方法中放入一个toast消息,但没有结果!你能帮忙吗?

1 个答案:

答案 0 :(得分:9)

进行双击的最简单方法是使用GestureDetector检测它。 “技巧”是确保将Activity的onTouchEvent委托给GestureDetector的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);
    }
}