简体   繁体   English

Android如何处理点击?

[英]How does Android handle clicks?

Definitely Android has some reliable way to determine what the user meant: a click or a scroll/movement. 无疑,Android具有确定用户含义的可靠方法:单击或滚动/移动。 Are there any simple rules that would be enough to build the same behaviour? 是否有任何简单的规则足以建立相同的行为?

Here is my problem. 这是我的问题。

I have a control that should be either clicked and run its own click handler, or scrolled (tap and move) and make the whole view scrolling (I have a custom view that scrolls in both directions, here is the idea if that matters). 我有一个控件,应该单击它并运行自己的单击处理程序,或者滚动(点按并移动)并使整个视图滚动(我有一个在两个方向上都滚动的自定义视图,如果有必要, 这里就是一个想法 )。 So I've set a simple touch listener that remembers the point in onTouch on MotionEvent.ACTION_DOWN and calls the click handler on MotionEvent.ACTION_UP if the point hasn't changed. 因此,我设置了一个简单的触摸侦听器,该侦听器可以记住onEvent上onTouch上的点,并在MotionEvent.ACTION_UP上调用单击处理程序(如果该点未更改)。

That worked fine in the emulator because mouse clicks are precise, so I haven't noticed any danger, and I didn't experience any problems on my Samsung Galaxy S Plus—perhaps I'm really accurate when tapping the screen. 这在模拟器中效果很好,因为鼠标单击非常精确,因此我没有注意到任何危险,并且在三星Galaxy S Plus上也没有遇到任何问题-也许我在点击屏幕时确实很准确。

However my first beta testers complained that the buttons don't work—not 100% but most times, so they needed to tap the button many times until it handles the click. 但是,我的第一个Beta测试人员抱怨按钮不起作用-不是100%而是大多数时间,因此他们需要多次单击按钮,直到它可以处理单击为止。 Some of them also told me that the buttons work slightly better if touching the screen with the very tip of the finger. 他们中的一些人还告诉我,如果用手指的指尖触摸屏幕,则按钮的效果会稍好一些。

After some thinking, I've assumed that they might move the finger a bit while clicking, so the points on DOWN and UP differed, and the handler didn't determine a click. 经过一番思考,我假设它们在单击时可能会稍微移动手指,因此DOWN和UP上的点有所不同,并且处理程序无法确定单击。 To verify this, I've added a simple improvement that allows a small drift—and this helped! 为了验证这一点,我添加了一个简单的改进,允许进行较小的漂移,这很有帮助! The testers confirmed that the trick makes things much better. 测试人员证实,这种技巧使事情变得更好。 Here is the final code of the handler: 这是处理程序的最终代码:

// This interface is for a real click handler that may differ depending on control.
interface Clicker {
    boolean click(View v);
}

class ButtonTouchListener implements OnTouchListener {
    private boolean _trackingButtonClick = false;
    private Clicker _clicker;
    // These two integers are the improvement.
    private int downX = 0;
    private int downY = 0;

    public ButtonTouchListener(Clicker clicker) {
        _clicker = clicker;
    }

    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = (int) event.getRawX();
                downY = (int) event.getRawY();
                trackingButtonClick = true;
                // Calling the onTouchEvent of the main view that handles the global scrolling.
                return onTouchEvent(event);
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_OUTSIDE:
                int newX = (int) event.getRawX();
                int newY = (int) event.getRawY();
                int xOffset = downX - newX;
                int yOffset = downY - newY;
                // These two lines are the improvement also.
                // 10 is a max move distance that I've simply guessed.
                if (Math.abs(xOffset) >= 10 || Math.abs(yOffset) >= 10)
                    _trackingButtonClick = false;
                // Calling the onTouchEvent of the main view that handles the global scrolling.
                return onTouchEvent(event);
            case MotionEvent.ACTION_UP:
                if (_trackingButtonClick) {
                    _trackingButtonClick = false;
                    // Calling the click handler of the control.
                    return _clicker.click((View)v.getTag());
                }
                _trackingButtonClick = false;
                return true;
            default :
                return onTouchEvent(event);
            }
        }
    }
}

So it looks like I've made a right guess, and Android likely has something similar inside. 因此,看来我做出了正确的猜测,而Android内部可能也有类似的东西。 But does it? 但是吗? Does anybody know? 有人知道吗 I'm not familiar with any sources of the platform and hope not to dig there, so I'll appreciate if someone shares the experience. 我对平台的任何来源都不熟悉,希望不要在该平台上挖掘,所以如果有人分享经验,我将不胜感激。

您在寻找GestureDetector吗?

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

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