繁体   English   中英

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

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

我想检查那个视图(无论是哪种,在我的情况下是ImageView)都没有被用户触摸/点击,我想不断地做。 我认为我应该使用某种线程,但我不知道如何开始。 我想要做的操作是隐藏ActionBar,当没有动作时我想隐藏它。
触摸视图时,我使用以下代码:

   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);

但是,当屏幕未触及时我必须做什么? :-)

创建一个额外的布尔值(例如:notTouched)为true,然后在onTouch覆盖中将其设置为false,然后在任何时候你都可以检查布尔值是true还是false。

编辑:如果你希望它在一段时间没有被触摸后隐藏它(如你所说)你可以在一个单独的线程上的循环中实现一个额外的计数器整数,如下所示:

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

在您的活动中添加一个标志:

boolean wasTouched = true;

添加一个计时器,检查视图是否被触摸:

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);

在onTouchListener中,将wasTouched设置为true:

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

这至少应该让你知道如何做到这一点。

暂无
暂无

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

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