我怎么能检测到屏幕上的点击?

时间:2022-05-08 21:11:03

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

更具体地说,我应该在哪里安装OnGestureListener,这样我就可以在屏幕上的任何地方检测到onSingleTapUp,即使ImageView占据了屏幕的一半。

Now I have the Listener on the Activity that has an ImageView.
But the Listener only fire when I click outside the ImageView.

现在我有了具有ImageView的活动的监听器。但是只有当我在ImageView之外点击时,监听器才会启动。

I read and try to understand this but cannot get it right.

我阅读并试图理解这一点,但无法正确理解。

this code is in the Activity.

此代码在活动中。

 public boolean onSingleTapUp(MotionEvent e) {
    //addtext.setText("-" + "SINGLE TAP UP" + "-");
    //Log.d(TAG, "- + SINGLE TAP UP + - ***********************************************************************");  
    int btnsize = buttonSave.getHeight();
    int viewWidth = display.getWidth();
    int viewHeight = display.getHeight();

    // RIGHT SIDE SCREEN
    if(e.getX()> (viewWidth*0.7)){
        Log.d(TAG, "RIGHT SIDE");
        if(e.getY()> viewHeight*0.7){         
            Log.d(TAG, "right down on screen");
        }else if(e.getY()> (viewHeight*0.45)){ 
            Log.d(TAG, "right middle on screen   ");
        }
    }
    // LEFT SIDE SCREEN
    if(e.getX()< (viewWidth*0.3)){
        Log.d(TAG, "LEFT SIDE");
        if(e.getY()> viewHeight*0.7){ 
            Log.d(TAG, "Left middle on screen  ");
        }else if(e.getY()> (viewHeight*0.45)){
            Log.d(TAG, "Left down on screen ");
        }
    }
    return true;
}

2 个解决方案

#1


7  

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:

    添加一个android:onClick值到你的基础/根布局(线性布局,相对论布局,等等)。你可以叫它任何你想叫的名字,我把它命名为一个例子:

    <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:

    将此方法添加到您的活动中,使用您为onClick指定的名称:

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

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

现在,点击屏幕将调用上面的方法。

#2


2  

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

我在回答我自己的问题。感谢上面的@Jason。我已经向所有视图注册了onTouchEvent。它的工作原理。

#1


7  

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:

    添加一个android:onClick值到你的基础/根布局(线性布局,相对论布局,等等)。你可以叫它任何你想叫的名字,我把它命名为一个例子:

    <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:

    将此方法添加到您的活动中,使用您为onClick指定的名称:

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

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

现在,点击屏幕将调用上面的方法。

#2


2  

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

我在回答我自己的问题。感谢上面的@Jason。我已经向所有视图注册了onTouchEvent。它的工作原理。