我正在设计一个带有自定义布局的媒体播放器。我希望界面在16秒不活动后消失。如果用户触摸屏幕,它应该重新出现。代码段如下:
public void showhideControllers(int n) {
if (n == 1) {
/* make layout invisible */
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
volumeBar.setVisibility(View.INVISIBLE);
audioControllView.setVisibility(View.INVISIBLE);
topBar.setVisibility(View.INVISIBLE);
}
}, 16000);
} else {
/* make layout visible */
volumeBar.setVisibility(View.VISIBLE);
topBar.setVisibility(View.VISIBLE);
audioControllView.setVisibility(View.VISIBLE);
showhideControllers(1);
}
}
@Override
public void onUserInteraction() {
super.onUserInteraction();
showhideControllers(2);
}
在onCreate()中,我通过调用showhideControllers(1);来启动计时器。 现在,当我点击屏幕时,布局重新出现并重置计时器。但是,如果我随机点击屏幕,则每次点击后计时器都不会重置,并且布局会在16秒后消失。 你能告诉我我做错了什么吗?
答案 0 :(得分:11)
很抱歉迟到的回复。但这是解决方案。我有类似的问题。所以我在您的代码中进行了以下更改,请尝试此操作并告诉我它是否对您有所帮助。
private Runnable hideControllerThread = new Runnable() {
public void run() {
volumeBar.setVisibility(View.GONE);
audioControllView.setVisibility(View.GONE);
topBar.setVisibility(View.GONE);
}
};
public void hideControllers() {
hidehandler.postDelayed(hideControllerThread, 15000);
}
public void showControllers() {
volumeBar.setVisibility(View.VISIBLE);
topBar.setVisibility(View.VISIBLE);
audioControllView.setVisibility(View.VISIBLE);
hidehandler.removeCallbacks(hideControllerThread);
hideControllers();
}
@Override
public void onUserInteraction() {
super.onUserInteraction();
if (audioControllView.getVisibility() == View.VISIBLE) {
hidehandler.removeCallbacks(hideControllerThread);
hideControllers();
} else {
showControllers();
}
}