如何在Android Studio中实现在特定时间后隐藏物品。

huangapple 未分类评论58阅读模式
标题翻译

how to hide things after certain time in android studio

问题

我在 Android Studio 中正在创建一个视频播放器。我希望在5秒后隐藏按钮、布局和媒体控制器,并且我正在使用手势来控制不同的属性,但问题是,当我在一定时间内应用2到3个手势时,5秒后按钮和媒体控制器开始闪烁。我在屏幕上使用以下代码:

  1. centerlayout.setOnTouchListener(new LinearLayout.OnTouchListener(){
  2. @Override
  3. public boolean onTouch(View v, MotionEvent m) {
  4. if (gestureDetectorc.onTouchEvent(m)) {
  5. if(m.getAction()==MotionEvent.ACTION_UP){
  6. handler.postDelayed(new Runnable() {
  7. @Override
  8. public void run() {
  9. hide();
  10. }
  11. },5000);
  12. }
  13. }
  14. return true;
  15. }
  16. });
英文翻译

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

  1. centerlayout.setOnTouchListener(new LinearLayout.OnTouchListener(){
  2. @Override
  3. public boolean onTouch(View v, MotionEvent m) {
  4. if (gestureDetectorc.onTouchEvent(m)) {
  5. if(m.getAction()==MotionEvent.ACTION_UP){
  6. handler.postDelayed(new Runnable() {
  7. @Override
  8. public void run() {
  9. hide();
  10. }
  11. },5000);
  12. }
  13. }
  14. return true;
  15. }
  16. });

答案1

得分: 0

Declare the runnable in global

  1. Runnable mRunnable = new Runnable() {
  2. @Override
  3. public void run() {
  4. hide();
  5. }
  6. };

call removeCallbacks before you call postDelay

  1. centerlayout.setOnTouchListener(new LinearLayout.OnTouchListener(){
  2. @Override
  3. public boolean onTouch(View v, MotionEvent m) {
  4. if (gestureDetectorc.onTouchEvent(m)) {
  5. if(m.getAction()==MotionEvent.ACTION_UP){
  6. handler.removeCallbacks(mRunnable);//add this
  7. handler.postDelayed(mRunnable, 5000);
  8. }
  9. }
  10. return true;
  11. }
  12. });
英文翻译

Declare the runnable in global

  1. Runnable mRunnable = new Runnable() {
  2. @Override
  3. public void run() {
  4. hide();
  5. }
  6. };

call removeCallbacks before you call postDelay

  1. centerlayout.setOnTouchListener(new LinearLayout.OnTouchListener(){
  2. @Override
  3. public boolean onTouch(View v, MotionEvent m) {
  4. if (gestureDetectorc.onTouchEvent(m)) {
  5. if(m.getAction()==MotionEvent.ACTION_UP){
  6. handler.removeCallbacks(mRunnable);//add this
  7. handler.postDelayed(mRunnable, 5000);
  8. }
  9. }
  10. return true;
  11. }
  12. });

huangapple
  • 本文由 发表于 2020年3月4日 03:30:17
  • 转载请务必保留本文链接:https://java.coder-hub.com/60514261.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定