英文:
how to hide things after certain time in android studio
问题
我在 Android Studio 中正在创建一个视频播放器。我希望在5秒后隐藏按钮、布局和媒体控制器,并且我正在使用手势来控制不同的属性,但问题是,当我在一定时间内应用2到3个手势时,5秒后按钮和媒体控制器开始闪烁。我在屏幕上使用以下代码:
centerlayout.setOnTouchListener(new LinearLayout.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent m) {
if (gestureDetectorc.onTouchEvent(m)) {
if(m.getAction()==MotionEvent.ACTION_UP){
handler.postDelayed(new Runnable() {
@Override
public void run() {
hide();
}
},5000);
}
}
return true;
}
});
英文:
I am creating a video player in android studio. I want to hide buttons, layout and media controller after 5 seconds and I am using gestures for different properties but the issue is that when I apply 2 to 3 gestures in a certain time, after 5 seconds the buttons and media controller start blinking. I use this code for stying on screen
centerlayout.setOnTouchListener(new LinearLayout.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent m) {
if (gestureDetectorc.onTouchEvent(m)) {
if(m.getAction()==MotionEvent.ACTION_UP){
handler.postDelayed(new Runnable() {
@Override
public void run() {
hide();
}
},5000);
}
}
return true;
}
});
答案1
得分: 0
Declare the runnable in global
Runnable mRunnable = new Runnable() {
@Override
public void run() {
hide();
}
};
call removeCallbacks
before you call postDelay
centerlayout.setOnTouchListener(new LinearLayout.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent m) {
if (gestureDetectorc.onTouchEvent(m)) {
if(m.getAction()==MotionEvent.ACTION_UP){
handler.removeCallbacks(mRunnable);//add this
handler.postDelayed(mRunnable, 5000);
}
}
return true;
}
});
英文:
Declare the runnable in global
Runnable mRunnable = new Runnable() {
@Override
public void run() {
hide();
}
};
call removeCallbacks
before you call postDelay
centerlayout.setOnTouchListener(new LinearLayout.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent m) {
if (gestureDetectorc.onTouchEvent(m)) {
if(m.getAction()==MotionEvent.ACTION_UP){
handler.removeCallbacks(mRunnable);//add this
handler.postDelayed(mRunnable, 5000);
}
}
return true;
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论