2014-12-13 75 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