zoukankan      html  css  js  c++  java
  • Android 用Activity的onTouchEvent来监听滑动手势

    package com.example.activityOnTouchEvent;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.MotionEvent;
    import android.widget.Toast;
    
    public class MyActivity extends Activity {
        //手指按下的点为(x1, y1)手指离开屏幕的点为(x2, y2)
        float x1 = 0;
        float x2 = 0;
        float y1 = 0;
        float y2 = 0;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            //继承了Activity的onTouchEvent方法,直接监听点击事件
            if(event.getAction() == MotionEvent.ACTION_DOWN) {
                //当手指按下的时候
                x1 = event.getX();
                y1 = event.getY();
            }
            if(event.getAction() == MotionEvent.ACTION_UP) {
                //当手指离开的时候
                x2 = event.getX();
                y2 = event.getY();
                if(y1 - y2 > 50) {
                    Toast.makeText(MyActivity.this, "向上滑", Toast.LENGTH_SHORT).show();
                } else if(y2 - y1 > 50) {
                    Toast.makeText(MyActivity.this, "向下滑", Toast.LENGTH_SHORT).show();
                } else if(x1 - x2 > 50) {
                    Toast.makeText(MyActivity.this, "向左滑", Toast.LENGTH_SHORT).show();
                } else if(x2 - x1 > 50) {
                    Toast.makeText(MyActivity.this, "向右滑", Toast.LENGTH_SHORT).show();
                }
            }
            return super.onTouchEvent(event);
        }
    
    
    }
  • 相关阅读:
    福大软工 · BETA 版冲刺前准备(团队)
    福大软工 · 第十一次作业
    Alpha 冲刺 (9/10)
    Alpha 冲刺 (8/10)
    Alpha 冲刺 (7/10)
    Alpha 冲刺 (6/10)
    Alpha 冲刺 (5/10)
    Alpha 冲刺 (4/10)
    福大软工1816 · 团队现场编程实战(抽奖系统)
    阿里八八β阶段Scrum(5/5)
  • 原文地址:https://www.cnblogs.com/wuyou/p/3675807.html
Copyright © 2011-2022 走看看