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

huangapple go评论86阅读模式
英文:

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

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

发表评论

匿名网友

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

确定