Android Click按钮在另一个按钮后面 [英] Android Click button behind another button

查看:133
本文介绍了Android Click按钮在另一个按钮后面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有6个按钮(绿色)可以打开应用程序.在此按钮的顶部,还有另一个按钮,可用于滑动监听器.

I have 6 Buttons (green) that open apps. On top of this buttons there is another button which I use for swipe listener.

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

If no swipe is detected (click) I want to click green buttons else perform swipe action triggered by red button.

这是我的TouchListener,摘录自此处

here is my TouchListener taken from 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() {}
}

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

This is where I initialize listener.

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() {}
});

滑动有效,但不能单击按钮.

Swipe works but cannot click buttons.

推荐答案

尝试在固定的3x3视图中使用GridView. 在那里,您可以在GridView上设置滑动手势,例如

Try to use GridView with fixed 3x3 view. There you can set swipe gesture on your GridView like

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)
                {
                }

            });

将来,您可以以更少的编码添加更多的按钮.

For future you can add more Buttons with very less coding.

这篇关于Android Click按钮在另一个按钮后面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆