Custom view … overrides onTouchEvent but not performClick

前端 未结 3 1066
半阙折子戏
半阙折子戏 2020-12-13 06:38

I get this warning (from the question title) in a custom Android view I am developing.

Why do I get warned? What\'s the logic behind it i.e. why is it a good
pra

3条回答
  •  佛祖请我去吃肉
    2020-12-13 06:56

    The onTouchEvent is not called by some Accessibility services, as explained by clicking the "more..." link in the warning details.

    It recommends that you override performClick for your desired action, or at least override it alongside your onTouchEvent.

    If your code is more fitting for the touch event, you can use something similar to:

    @Override
    public boolean performClick() {
        if (actionNotAlreadyExecuted) {
            MotionEvent myEvent = MotionEvent.obtain(long downTime, long eventTime, int action, float x, float y, int metaState);
            onTouch(myView, myEvent);
        }
        return true; // register it has been handled
    }
    

    More information on accessing touch events through code is available at trigger ontouch event programmatically

提交回复
热议问题