简体   繁体   English

如何检测未触摸/单击视图?

[英]How to detect that View is NOT touched/clicked?

I want to check that view (no matter which, in my case ImageView) is not touched/clicked by user, and I want to do it constantly. 我想检查那个视图(无论是哪种,在我的情况下是ImageView)都没有被用户触摸/点击,我想不断地做。 I think that i should use some kind of thread but I have no idea how to start. 我认为我应该使用某种线程,但我不知道如何开始。 The operation which I want to do is hiding the ActionBar, when there is no action I want to hide it. 我想要做的操作是隐藏ActionBar,当没有动作时我想隐藏它。
When view is touched I use this code: 触摸视图时,我使用以下代码:

   Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        public void run() {

            //if (registerImageViewCallback(slidesPagerAdapter);
            gestureImageView = slidesPagerAdapter.getGestureImageView();
            gestureImageView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    getSupportActionBar().show();
                    return false;
                }
            });
        }
    }, 10);

But what I have to do when screen is not touched? 但是,当屏幕未触及时我必须做什么? :-) :-)

Create an additional boolean (ex: notTouched) to true, then set it to false inside your onTouch override, then at any point you can just check if the boolean is true or false. 创建一个额外的布尔值(例如:notTouched)为true,然后在onTouch覆盖中将其设置为false,然后在任何时候你都可以检查布尔值是true还是false。

EDIT : If you want it to hide it after it has not been touched for a while (as you said) you could implement an additional counter integer in a loop on a separate thread like this: 编辑:如果你希望它在一段时间没有被触摸后隐藏它(如你所说)你可以在一个单独的线程上的循环中实现一个额外的计数器整数,如下所示:

while(true) {
     if (notTouched) {
         counter++
         if(counter == 20) {  // Assuming 20 seconds have passed of not touching
             hideMethodHere(); // Execute hiding
         }
         Thread.sleep(1000); // Sleep for a second (so we dont have to count in miliseconds)
     } else {
     counter = 0; // If it was touched, reset the counter
     notTouched == true; // Reset the touch flag as well because otherwise it will be visible forever if you touch it
     }
}

Add a flag to your Activity: 在您的活动中添加一个标志:

boolean wasTouched = true;

Add a timer that checks if the view was touched: 添加一个计时器,检查视图是否被触摸:

Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
   @Override
   public void run() {
      if ( wasTouched == false ) {
         runOnUiThread(new Runnable() {
             public void run(){
               hideYourActionBar();
             }      
          });
      } else {
        wasTouched = false;
      }
   }    
 }, 0, YourDelayInMsHere);

In your onTouchListener, set wasTouched to true: 在onTouchListener中,将wasTouched设置为true:

public boolean onTouch(View v, MotionEvent event) {
   wasTouched = true;
}

This should at least give you an idea of how to do it. 这至少应该让你知道如何做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM