我想检查该 View (无论是哪个 View ,在我的例子中是 ImageView)未被用户触摸/点击,我想经常这样做。我认为我应该使用某种线程,但我不知道如何开始。我想做的操作是隐藏ActionBar,当没有 Action 时我想隐藏它。
触摸 View 时,我使用此代码:
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);
但是当屏幕未被触摸时我必须做什么? :-)
最佳答案
创建一个额外的 boolean 值(例如:notTouched)为真,然后在您的 onTouch 覆盖中将其设置为假,然后您可以随时检查 boolean 值是真还是假。
编辑: 如果您希望它在一段时间未被触摸后隐藏它(如您所说) 您可以像这样在单独线程的循环中实现一个额外的计数器整数:
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
}
}
关于java - 如何检测未触摸/点击 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21134056/