简体   繁体   English

如何在我的应用程序中实现后退按钮手势

[英]How to implement back button gesture in my application

As we know from android OS version 4.1 there is a new feature to add a back button gesture for the device. 我们从android OS 4.1版中知道,有一项新功能可为设备添加后退按钮手势。 I want to use this back button gesture from my application. 我想在我的应用程序中使用此后退按钮手势。 How to do that programmatically? 如何以编程方式做到这一点?

You can implement gesture in application programmatically and on particular gesture event you can call onBackPressed(); 您可以以编程方式在应用程序中实现手势,并且可以在特定手势事件上调用onBackPressed(); method of activity. 活动方法。 This way you will able to achieve back button gesture in your application. 这样,您将能够在应用程序中实现后退按钮手势。

import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class OnSwipeTouchListener implements OnTouchListener {

private final GestureDetector gestureDetector;

public OnSwipeTouchListener (Context ctx){
    gestureDetector = new GestureDetector(ctx, new GestureListener());
}

private final class GestureListener extends SimpleOnGestureListener {

    private static final int SWIPE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        boolean result = false;
        try {
            float diffY = e2.getY() - e1.getY();
            float diffX = e2.getX() - e1.getX();
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        onSwipeRight();
                    } else {
                        onSwipeLeft();
                    }
                }
            } else {
                if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeBottom();
                    } else {
                        onSwipeTop();
                    }
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}

public void onSwipeRight() {
    onBackPressed();
}

public void onSwipeLeft() {
    onBackPressed();
}

public void onSwipeTop() {
    onBackPressed();
}

public void onSwipeBottom() {
    onBackPressed();
}
}

Let me know if you face any issue. 让我知道您是否遇到任何问题。

You don't have to worry about that. 您不必为此担心。 If the user has this accessibility feature enabled, when he/she does the gesture, android will automatically simulate a back button press. 如果用户启用了此辅助功能,则当他/她进行手势操作时,Android会自动模拟后退按钮的按下。

I might have completely misunderstood your question, in which case you should consider revising. 我可能完全误解了您的问题,在这种情况下,您应该考虑进行修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM