OnTouch关于performClick的Warning

来源:互联网 发布:数据库的定义及特点 编辑:程序博客网 时间:2024/06/16 08:15

OnTouch关于performClick的Warning

当你对一个控件(例如FloatingActionButton)使用setOnTouchListener() 或者是对你的自定义控件重写onTouchEvent方法时会出现这个警告,警告内容全文如下

If a View that overrides onTouchEvent or uses an OnTouchListener does not also implement performClick and call it when clicks are detected, the View may not handle accessibility actions properly. Logic handling the click actions should ideally be placed in View#performClick as some accessibility services invoke performClick when a click action should occur.

大概的意思是说

如果一个覆盖了onTouchEvent方法或者使用了OnTouchListener的控件没有引用performClick并且调用它的点击事件被拦截,控件可能不能合适的处理访问操作。逻辑上来讲处理点击操作应该更合理的被放置在View#performClick中,这样当点击事件发生时一些访问性操作可以成功请求调用performClick。

这段话各种从句太多了,费了好大劲才弄懂什么意思翻译出来。

可能意思还是不太明了,再说明一下:当你添加了一些点击操作,例如像setOnClickListener这样的,它会调用performClick才可以完成操作,但你重写了onTouch,就有可能把performClick给屏蔽了,这样这些点击操作就没办法完成了,所以就会有了这个警告。

一般来说这个警告都是可以忽略不用管他的,但是如果强迫症看着难受的话可以使用下面的方法来解决这个问题。

if (action == MotionEvent.ACTION_DOWN) {        performClick(); // Call this method to handle the response, and        // thereby enable accessibility services to        // perform this action for a user who cannot        // click the touchscreen.

写全一点

public boolean onTouchEvent(MotionEvent ev) {        switch (ev.getAction()) {                case MotionEvent.ACTION_DOWN:                    performClick();                    break;                case MotionEvent.ACTION_UP:                case MotionEvent.ACTION_CANCEL:                    break;            }        return true;    }

这样应该就OK了。

原创粉丝点击