@Override
public void onBackPressed() {
super.onBackPressed();
// 退出程序的操作
finish();
}
private static final long DOUBLE_BACK_PRESS_TIME = 2000; // 雙擊返回鍵的間隔時間
private long backPressTime;
@Override
public void onBackPressed() {
if (backPressTime + DOUBLE_BACK_PRESS_TIME > System.currentTimeMillis()) {
super.onBackPressed();
// 退出程序的操作
finish();
} else {
Toast.makeText(this, “再次點擊返回鍵退出程序”, Toast.LENGTH_SHORT).show();
}
backPressTime = System.currentTimeMillis();
}
private static final int SWIPE_THRESHOLD = 100; // 手勢的閾值
private static final int SWIPE_VELOCITY_THRESHOLD = 100; // 手勢的速度閾值
private GestureDetector gestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 初始化GestureDetector
gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// 計算手勢的速度和方向
float deltaX = e2.getX() - e1.getX();
float deltaY = e2.getY() - e1.getY();
if (Math.abs(deltaX) > Math.abs(deltaY) && Math.abs(deltaX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (deltaX > 0) {
// 右滑動,退出程序的操作
finish();
}
}
return super.onFling(e1, e2, velocityX, velocityY);
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// 將觸摸事件傳遞給GestureDetector處理
gestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}