繁体   English   中英

Android - 如何处理按钮上的向右或向左滑动手势

[英]Android - How to handle swipe right or left gestures on buttons

我做了一个简单的键盘,我想要的是当用户将手指拖到右侧的键上时,我写了一些代码,但是当空白时可以使用。 它在像这张图片这样的键上不起作用

这里是我的主类,我使用了 OnGestureListener 和 OnClickListener 接口我也保存了 x,y,但这种模式适用于页面的空白部分

1

    public class MainActivity extends AppCompatActivity implements View.OnClickListener , GestureDetector.OnGestureListener {

    private float x1 , x2 , y1 , y2;
    private static final int MIN_DISTANCE = 150;
    private GestureDetector gestureDetector;

    private Button button_00 , button_0 , button_1 , button_2 , button_3 , button_4 , button_5 , button_6 , button_7 , button_8 , button_9 ;
    private Button button_10 , button_11 , button_12;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.gestureDetector = new GestureDetector(MainActivity.this , this);

        initButtons();
        

        
    }

    private void initButtons() {
        button_00 = findViewById(R.id.button_00);
        button_0 = findViewById(R.id.button_0);
        button_1 = findViewById(R.id.button_1);
        button_2 = findViewById(R.id.button_2);
        button_3 = findViewById(R.id.button_3);
        button_4 = findViewById(R.id.button_4);
        button_5 = findViewById(R.id.button_5);
        button_6 = findViewById(R.id.button_6);
        button_7 = findViewById(R.id.button_7);
        button_8 = findViewById(R.id.button_8);
        button_9 = findViewById(R.id.button_9);
        button_10 = findViewById(R.id.button_10);
        button_11 = findViewById(R.id.button_11);
        button_12 = findViewById(R.id.button_12);
        

    }
    
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        gestureDetector.onTouchEvent(event);

        switch (event.getAction()) {

            // Starting to swipe time gesture
            case MotionEvent.ACTION_DOWN :
                x1 = event.getX();
                y1 = event.getY();
                break;
            // Ending time swipe gesture
            case MotionEvent.ACTION_UP :
                x2 = event.getX();
                y2 = event.getY();

                // Getting value for horizontal swipe
                float valueX = x2 - x1;
                // Getting value for vertical swipe
                float valueY = y2 - y1;

                // Left and Right
                if (Math.abs((valueX)) > MIN_DISTANCE) {

                    // Detect left to right swipe
                    if (x2>x1)
                        Toast.makeText(this, "Right", Toast.LENGTH_SHORT).show();
                    else
                        Toast.makeText(this, "Left", Toast.LENGTH_SHORT).show();
                }
               
        }
        return super.onTouchEvent(event);
    }

    @Override
    public void onClick(View v) {

    }

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

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        return false;
    }
}

您可以为此使用 View Pager 组件

这是了解如何创建视图寻呼机的链接: https : //developer.android.com/guide/navigation/navigation-swipe-view-2

暂无
暂无

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

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