How can I detect tap on the screen?

后端 未结 2 1632
遥遥无期
遥遥无期 2021-01-12 01:06

More specific where do I attach OnGestureListener so that
I can detect onSingleTapUp everywhere on the screen,
even if an ImageView

相关标签:
2条回答
  • 2021-01-12 01:58

    Detect Screen Tap

    I'm answering this for those who just need a simple way to detect a tap on the screen:

    1. Add an android:onClick value to your base/root layout (LinearLayout, RelativeLayout, etc.). You can call it anything you want, I'm naming it screenTapped as an example:

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools" 
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:onClick="screenTapped">
      
    2. Add this method to your Activity using the same name you specified for onClick:

      public void screenTapped(View view) {
          // Your code here
      }
      

    Now, tapping on the screen will call the method above.

    0 讨论(0)
  • 2021-01-12 02:09

    I'm answering my own question. Thanks to the above @Jason. I have registered onTouchEvent to all views.
    It works great.

    0 讨论(0)
提交回复
热议问题