How can I show a toast for a specific duration?

前端 未结 12 2197
自闭症患者
自闭症患者 2020-12-08 01:08

This is the way I have to show the Toast for 500 milliseconds. Though, it\'s showing more than a second.

Toast.makeText(LiveChat.this, \"Typing\         


        
12条回答
  •  余生分开走
    2020-12-08 01:29

    This one is working fine for me.

    final Toast mToastToShow;
                int toastDurationInMilliSeconds = 10000;
                mToastToShow =  Toast.makeText(getApplicationContext(), "Snapshot Saved Successfully.",Toast.LENGTH_LONG);
    
    
                // Set the countdown to display the toast
                CountDownTimer toastCountDown;
                toastCountDown = new CountDownTimer(toastDurationInMilliSeconds, 1000 /*Tick duration*/) {
                    public void onTick(long millisUntilFinished) {
                        mToastToShow.show();
                    }
                    public void onFinish() {
                        mToastToShow.cancel();
                    }
                };
    
                // Show the toast and starts the countdown
                mToastToShow.show();
                toastCountDown.start();
    

    the countdown is used to display a toast message for a specific duration.

提交回复
热议问题