1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Android】OnTouchListener使用した時の警告への対応【Kotlin】

Posted at

はじめに

OnTouchListenerを使っていると

Custom view ConstraintLayout has setOnTouchListener called on it but does not override performClick

という警告が出ます。
このままでも実行するのに問題はありませんが、OnTouchListener内が全て黄色く警告が出たままになってしまうので、いい気はしませんね。
そこでこの警告を消す方法を記事に残します。

アノテーションを付ける

OnTouchListenerを記述しているonViewCreatedメソッドなどの上に

@SuppressLint("ClickableViewAccessibility")

をつけることで警告を消すことができます。
一番簡単な方法ですが、暫定的な解決にしかなりません。

Overrideする

警告を直訳すると

カスタムビュー ConstraintLayout には setOnTouchListener が呼び出されていますが、 performClick はオーバーライドされません

ということなので カスタムViewを作りそのカスタムView内でperformClick()をOverrideします

  override fun performClick() {
        super.performClick()
        return true
    }

performClick()を使用する

そもそもperformClick()は ボタンが押された時以外でも、コードでクリック操作を実行できるもので、これをそのまま使用することで警告が消えることがあります。

view.performClick()

これは警告に有無に関わらず、今後役に立ちそうな事なので覚えておきたいですね。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?