【问题标题】:Android Click button behind another button另一个按钮后面的Android Click按钮
【发布时间】:2017-09-21 21:27:44
【问题描述】:

我有 6 个可打开应用的按钮(绿色)。在此按钮之上还有另一个按钮,我用于滑动监听器。

如果没有检测到滑动(点击),我想点击绿色按钮,否则执行由红色按钮触发的滑动操作。

这是我的 TouchListener 取自 here

public class OnSwipeTouchListener implements OnTouchListener {

    private final GestureDetector gestureDetector;

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

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }

    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();
                        }
                        result = true;
                    }
                }
                else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeBottom();
                    } else {
                        onSwipeTop();
                    }
                    result = true;
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }
    }

    public void onSwipeRight() {}
    public void onSwipeLeft() {}
    public void onSwipeTop() {}
    public void onSwipeBottom() {}
    public void onTap() {}
}

这是我初始化监听器的地方。

Button ok = (Button) findViewById(R.id.btn_hex_over);
final Context c = context;

ok.setOnTouchListener(new OnSwipeTouchListener(getBaseContext()) {
    public void onSwipeTop() {}
    public void onSwipeBottom() {}
    public void onTap() {}
});

滑动有效,但无法点击按钮。

【问题讨论】:

  • 你的 onClickListener 在哪里?
  • GestureListener 不需要红色按钮。一个简单的布局也可以完成这项工作。
  • @MuratK。但我需要一个全表面的听众,而不仅仅是按钮

标签: android ontouchlistener gesturedetector


【解决方案1】:

尝试将GridView 与固定的 3x3 视图一起使用。 在那里你可以在你的GridView 上设置滑动手势

grid.setOnTouchListener(new OnSwipeTouchListener(mContext)
        {

            public void onSwipeRight()
            {

            }

            public void onSwipeLeft()
            {

            }

        });

你也可以设置

    grid.setOnItemClickListener(new AdapterView.OnItemClickListener()
            {
                @Override
                public void onItemClick(AdapterView<?> view, View cell, int position, long id)
                {
                }

            });

将来您可以用更少的代码添加更多的按钮。

【讨论】:

  • 谢谢,我一接触就尝试。绿色按钮已经在 GridView 中,所以我只需要把监听器放在里面。
  • @CBeTJlu4ok 如果这对您的要求有帮助,您可以接受答案
猜你喜欢
  • 1970-01-01
  • 2016-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多