2014-12-13 34 views
18

我在我正在开发的自定义Android视图中收到此警告(来自问题标题)。自定义视图...覆盖onTouchEvent但不执行点击

为什么我会收到警告?它背后的逻辑是什么,即为什么它是一个很好的
练习也覆盖performClick当你重写onTouchEvent

+0

http://stackoverflow.com/questions/24952312/ontouchlistener-warning- ontouch-should-call-viewperformclick-when-a-click-is-d,http://android-er.blogspot.fr/2014/09/warning-custom-view-overrides.html – 2014-12-13 18:56:59

+0

@shayanpourvatan我看到了这些链接。但他们与我的问题不一样。 – 2014-12-13 21:54:42

+0

@ peter.petrov他们完全一样。他们都有同样无用的答案 - 没有什么可处理的,而performClick()似乎没有什么用处。我现在决定为此压制林特警告。 – 2018-03-09 08:33:54

回答

12

此警告告诉您覆盖performClick

@Override 
public boolean performClick() { 
    // Calls the super implementation, which generates an AccessibilityEvent 
     // and calls the onClick() listener on the view, if any 
     super.performClick(); 

     // Handle the action for the custom click here 

     return true; 
} 

但它不是强制性的。由于我已经创建了一个自定义的knobView,并且在我正面临此警告的情况下工作得非常好。

+0

关键在于您只有注释“处理自定义点击操作”的部分。我应该在那里处理?我认为没有用处。我不想写一些无用的代码,只是把Lint写得很糟糕。 – 2018-02-20 10:55:32

5

虽然这只是一个警告,可以忽略,但它似乎是可访问性所必需的。

详细描述here

然后,当你管理一个动作,你应该添加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. 
+0

不幸的是,博客并没有真正解释任何事情。我有一个片段,你可以“画”你的签名。我不明白为什么有人会从没有做任何事情的点击处理程序中受益。 :(我不想写愚蠢的代码来关闭Lint。 – 2018-02-20 10:51:35